You are on page 1of 42

PESIT – Dept.

D off MCA

Programming
Concepts & C
Subject Code: MCA 14 Language.

Faculty: Mr. N. Guruprasad


Head – Dept.
Dept of MCA

http://www.pes.edu
PESIT – Dept. of MCA • Computer Concepts and
Programming Concepts
• Introduction of C
• Fundamental Of C
• Preprocessor
Overview of flow of • Decision Control
Structures
Subject • Loop Control Structures
• Functions Storage
g
Classes
• Arrays and Pointers
• Strings
• Structure and Unions
• Bitwise Operators
• File Handling
g

http://www.pes.edu
PESIT – Dept.
Dept of MCA

• Introduction to Computers.
• C Programming
g g Language.
g g
• Data Structures and Algo.
• OS Concepts.
CORE COMPETENCE • UNIX OS.
• Object Oriented Programming
• C++ Programming Language

From Here?

S/W Engg. Graphics Network

http://www.pes.edu
PESIT – Dept.
Dept of MCA
Problem

gy
Solution Methodology
( Algorithm)

Programming Language
( C, C++, etc.,)

Program + Data
Flowchart to go
through problems Solve

Example Result

http://www.pes.edu
PESIT – Dept.
Dept of MCA ALGOL 60
(International Committee -1960)

CPL
(Cambridge & Univ. Of London-1963)

BCPL
DEVELOPMENT OF C (Martin Richards-cambridge-1967)

B
(Ken Thompson,Bell Labs.1970)

C
(Dennis Ritchie,bell Labs.1972)

http://www.pes.edu
PESIT – Dept.
Dept of MCA • H
H.L.L:
L L: Better program
efficiency. Ex: Fortran etc.,
• L.L.L.: Better machine
efficiency. Ex: Assembly etc.,

Where does C stands ?


LANGUAGES
PROGRAMMING
LANGUAGES HIGH LEVEL C LOW LEVEL
( Problem Oriented ) ( Machine Oriented )

• Has relatively good


programming efficiency,
efficiency
compared to L.L.L.
• Has relatively good machine
efficiency, compared to H.L.L.

http://www.pes.edu
PESIT – Dept. of MCA EDITOR

C SOURCE CODE

C PREPROCESSOR

STAGES IN C COMPILER

COMPILATION OF C ASSEMBLY LANG. CODE


PROGRAM
ASSEMBLER

OBJ CODE & LIB FILES

LINKER

EXECUTABLE CODE

LOADER
http://www.pes.edu
PESIT – Dept.
Dept of MCA

• No input output operations.


• It is a language of
functions data types
functions, types,
assignments and flow
controls.
CHARACTERISTICS • C iss noted
oted for
o its
ts ab
ability
ty to
perform pointer arithmetic.
OF C
• C functions are recursive by
default.
• The code in C is grouped
into blocks.
• C allows you to develop a
program in i multiple
lti l source
files that are independently
compiled.

http://www.pes.edu
PESIT – Dept.
Dept of MCA

A fundamental does not


differ from language to
language. It still remains
same. What differs is the
syntax.

C Character set:
FUNDAMENTALS OF C
• Character set A-Z
• Character set a-z
• Digits 0-9
• Special
p Characters like *,, +,, -
, ( , ) , < , > etc.,

http://www.pes.edu
PESIT – Dept.
Dept of MCA

Identifiers: name given to


various program elements.
Ex: variables, functions,
arrays. First character must
always be a letter or
FUNDAMENTALS underscore. Ex: area, tax_rate.

OF C Keywords: Words which are


standard and have predefined
meaning in C.Ex:
C Ex: break,
break
printf, switch.

Constant: C constant are also


referred as data types. Two
types of constant: primary
and secondary.

http://www.pes.edu
PESIT – Dept.
Dept of MCA Integer Constant:
• Must have at least one
digit.
• Should not contain decimal
point.
• Could be either +ve or _ve.
TYPES OF C
CONSTANTS Real Constants: Two forms
1. Exponential form:
mantissa & exponent , they
should be separated by
letter e.
2. Fractional form

Character constant:
single character/digit. Ex.
‘A’ max length is one.
http://www.pes.edu
PESIT – Dept.
Dept of MCA

Values varies during program


execution.

Integer variables int n;


Real variable float n;
Ch
Character variable
i bl char
h n;
VARIABLES OF C
Rules:
• Length 1 to 8 char (depends)
• First char should be alphabet.
• No commas, no blank spaces.
• No special symbols, only
underscore.

http://www.pes.edu
PESIT – Dept.
Dept of MCA

Achieved with the help of library


Functions.

scanf() :
I/O
/ OPERATIONS Allows you to input data from
standard
d d input
i device.
d i
IN C Ex: scanf(“%d”, &var);

printf():
Allows you to output data on
standard output device.
Ex: printf(“%d”, var);

http://www.pes.edu
PESIT – Dept.
Dept of MCA How does C program look like?

/* Sample Program */
main(){
i (){
printf(“\n Welcome to C”);
}

STRUCTURE OF C main(): Where C prg starts


PROGRAM executing.
\n: New line character.
{ }: Analogous to begin &
end in PASCAL.
/* */: Comment statement

Spacing should be provided


from the point of readability.
http://www.pes.edu
PESIT – Dept. of MCA Operators:
p

Operations C PASCAL
Addition + +
Subtraction - -
Multiplication * *
Division / DIV
OPERATORS AND ITS Remainder % MOD
HIERARCHY
Ex: on=ink
on=ink*act/2+3/2*act+2+t;
act/2+3/2 act+2+t;
ink=3, act=2, t=2.2;
O/P: on=9.2

Ex: s=qui*add/4-6/2+2/3*6/go;
qui=2, add=4, go=3;
O/p: s=-1
s= 1

http://www.pes.edu
PESIT – Dept. of MCA

Ex: a=7//2*(3.14+2)*3/5

OPERATORS AND ITS


HIERARCHY

http://www.pes.edu
PESIT – Dept.
Dept of MCA OPERATIONS C PASCAL

greater than > >

greater than >= >=


or equal to

RELATIONAL less than < <


OPERATORS
less than <= <=
or equal to

equal to == =

not equal to != <>

http://www.pes.edu
PESIT – Dept.
Dept of MCA OPERATIONS C PASCAL

Negation ! NOT
L i l AND
Logical && AND
Logical OR || OR
LOGICAL
S
Synopsis
i off LLogical
i l AND & OR.
OR
OPERATORS
T– True F– False

LOGICAL AND LOGICAL OR

T && T = T T || T = T
T && F = F T || F = T
F && T = F F || T = T
F && F = F F || F = F
http://www.pes.edu
PESIT – Dept.
Dept of MCA Increment operators:
a = a + 1 is same as a+=1
OR
a++ (post
( t iincrement)
t) or
INCREMENT ++a (pre increment)
OPERATOR Similarly decrement operator.
C be
Can b used d for
f prefix
fi or suffix.
ffi
AND
FORMAT Format Specification:
%d integer
SPECIFICATION %f real, float
%c character
%s string
%o octal
%x hexadecimal
%u unsigned integer
http://www.pes.edu
PESIT – Dept.
Dept of MCA
Problem
P bl 1:
1
main(){
int x;
x=-3+4*5-6;

? printf(“\n x=%d”,
xx=3+4%5-6;
3+4%5 6;
printf(“\n x=%d”,
x);

x);
VALIDITY OF CODE x=-3*4%-6/5;
printf(“\n
i tf(“\ x=%d”,
%d” x);
)
x=(7+6)%5/2;
printf(“\n x=%d”, x);
}

Sol: x=11,
x 11, x
x=1,
1, x
x=0,
0, x
x=1
1

http://www.pes.edu
PESIT – Dept. of MCA
Problem 2:
main(){
printf(“\n %d %o
%x”,72,72,72);
%x 72 72 72);

? }

Sol: 72, 110(Octal),48(Hex).


VALIDITY OF CODE
Problem 3:
main(){
int a=5.99999, b=5.00001;
printf(“ a=%d b=%d
printf( b=%d”, a,b);
a b);
}
Sol: a=5 b=5.

http://www.pes.edu
PESIT – Dept. of MCA

Problem 4:

main(){

? printf(“\n
printf(“\n
printf(“\n
%d”,
%d”,
%d”,
4/3);
4/-3);
-4/3);
VALIDITY OF CODE printf(“\n %d”, -4/-3);
}

Sol: 1, -1, -1, 1;

Note: One of them is –ve it


will give –ve result.

http://www.pes.edu
PESIT – Dept.
Dept of MCA
P bl
Problem 5:
5

main(){
printf(“\n %d”, 4%3);

? printf(“\n
printf(“\n
printf( \n
printf(“\n
%d”,
%d
%d”,,
%d”,
4%-3);
-4%3);
4%3);
-4%-3);
VALIDITY OF CODE }

Sol: 1, 1, -1, -1

Note: one or both –ve result


takes sign of the numerator.

http://www.pes.edu
PESIT – Dept.
Dept of MCA
Problem
P bl 6:
6
main(){
printf(“ Hello, world
“ );

? }
Sol: Compilation error
Unterminated String / Char
VALIDITY OF CODE Const.
Problem 7:
main(){
printf(“ Hello world” )
;
}
Sol: Hello world

http://www.pes.edu
PESIT – Dept.
Dept of MCA
Problem
P bl 8:
8
main(){
int i = 5;
printf (“%d“, i = i == 6 );

? }
Sol: 0

VALIDITY OF CODE Problem 9:


Will there
th be
b any error?
?
main(){
{ {
} }
}
Sol: No

http://www.pes.edu
Problem
P bl 10 :
PESIT – Dept.
Dept of MCA

main(){
int a,b=2,c;

? a=2*(b++);
c=2*(++b);
VALIDITY OF CODE printf (“ %d %d
%d“, a,b,c);
}
Sol: 4 4 8

http://www.pes.edu
P bl
Problem 11 :
PESIT – Dept.
Dept of MCA

main(){
i (){

?
int k=35;
printf(“\n %d %d %d “,
k==35,k=50,k>40);
}
VALIDITY OF CODE

Sol: 0 50 0

http://www.pes.edu
PESIT – Dept.
Dept of MCA 1) How should I decide
which integer type to
use
2) Under my compiler this
code prints 49
int i=7;
printf(“%d”,i++
p ( , * i++);
);
HOW ABOUT
Regardless of the order
of evaluation, should’nt
it print 56.
ASSIGNMENTS 3) Is it safe to assume
that the right hand side
of the && and ||
operators won
won’tt be
evaluated if the left
hand side determines
the outcome.

http://www.pes.edu
PESIT – Dept.
Dept of MCA
4) P
Peoplel keep
k saying
i
that the behavior of i =
i++ is undefined, but I
just tried it on an ANSI
conforming compiler
and got the results I
HOW ABOUT expected.
5) If I’m not using the
value of the
ASSIGNMENTS expression, should I
use i++ or ++I to
( cont ) increment a variable.
6) Y does’nt
does nt this code
work :
int a=1000,b=1000;
long int c
c=a*b;
a b;

http://www.pes.edu
PESIT – Dept.
Dept of MCA
7) HHow do
d I round d off
ff th
the
numbers.
8) Y does’nt C have an
exponentiation
ti ti
operator.
9) What is top down
HOW ABOUT d i
design methodology.
th d l
What R its advantages.
10) How does operation of
ASSIGNMENTS an interpreter
i t t differ
diff
(cont) from compiler.

http://www.pes.edu
PESIT – Dept.
Dept of MCA

“ It is a program that
preprocesses the source
code, before it passes
through compiler. Operates
under what is called as
P
Preprocessor C
Command d lines
li
PREPROCESSOR or Directives ”

Begins with #(hash) symbol


and do not requires
semicolon at the end.

http://www.pes.edu
PESIT – Dept. of MCA
Grouped under three categories:
• File Inclusion Directives :
Achieved by # include.
Syntax: #include<filename>
Ex: #include<stdio.h>
• Macro Substitution :
PREPROCESSOR Identifier in a program is
replaced by some predefined
strings composed of tokens.
Ex: #define MAX 100
• Compiler Control :
Selectivelyy removingg the code
sections or choosing form two
sections. Such process called as
Conditional Compilation.

http://www.pes.edu
PESIT – Dept. of MCA Ex: - - -
main(){
---
#ifdef IBM-PC {
---
--- /* code for IBM-
PC *//
PREPROCESSOR }
#else{
---
--- /* code for HP */
}
#endif
}
To run prg on IBM-PC we include:
#define IBM
IBM-PC.
PC.

http://www.pes.edu
P bl
Problem 1:
PESIT – Dept. of MCA

?
#define PRODUCT(x) (x*x)
(x x)
main(){
int i=3,j;
VALIDITY OF CODE j PRODUCT(i+1);
j=PRODUCT(i+1);
printf(“\n %d “,j);
}

Sol : 7

http://www.pes.edu
P bl
Problem 2:
PESIT – Dept. of MCA

?
#define PRODUCT(x) (x*x)(x x)
main(){
int i=3,j,k;
VALIDITY OF CODE j PRODUCT(i++);
j=PRODUCT(i++);
k=PRODUCT(++i);
printf(“\n %d %d“,j,k);
}

Sol : 9 49

http://www.pes.edu
PESIT – Dept. of MCA
What do U mean by a platform.
How do U classify computer
according to logic use.
How do U classify computer
according to size.
Diff. b/w :
Q
Questions
ti RAM & ROM
PROM & EPROM
Different generations
Batch & Transaction processing
Hardware
Software & its classification

http://www.pes.edu
PESIT – Dept. of MCA
Internet
What it is
Who made it
WWW / Web server etc
Internet & Intranet

Q
Questions
ti ( contt )

http://www.pes.edu
PESIT – Dept. of MCA
Explain the organization of the
digital computer.
What is the need for secondary
storage devices. Write note on
hard disk.
Explain the working principle of
different
d e e t types oof p
printers.
te s
A i
Assignments
t :
What is an O.S. Explain its
various functions.
Diff b/w :
Diff.
Assemble & HLL
What is Computer N/w. Explain
briefly different types of nw
nw.

http://www.pes.edu
PESIT – Dept. of MCA
Differentiate between primary
memory and secondary
memory.
Explain input and output
devices along with examples.
Define :
A i
Assignments
t ( contt ) : OS
O.S
Compiler
Interpreter
Editor
Edit
Explain features of UNIX O.S
Note on Internet & E-mail.

http://www.pes.edu
PESIT – Dept. of MCA Logic is very important in
programming.

Consider the following


procedure of washing
clothes using a washing
machine.

Fl
Flowchart
h t Example
E l : 1) Put
P the
h clothes
l h ini the
h
washtub.
2) Pour water.
3) Pour detergent powder.
powder
4) Switch on the washing
machine.
5) Set the timer and wait
f a few
for f minutes.
i t
6) Drain the water out.
7) End.

http://www.pes.edu
PESIT – Dept. of MCA This procedure gets the
workk done.
d

Now, suppose the same


steps are performed in a
slightly different order.

1) Put the clothes in the


Fl
Flowchart
h t Example
E l washtub.
h b
2) Switch on the washing
( cont ) : machine.
3) Set the timer and wait
for a few minutes.
4) Pour water.
5) Pour detergent powder.
6) Drain the water out.
7) End.

http://www.pes.edu
PESIT – Dept. of MCA
In this case,
case your clothes
may tear off, since you
have switched on the
washing machine before
pouring water into it.

Fl
Flowchart
h t Example
E l If you apply the above
analogy to programming,
( cont ) : you should carefully think
about the order in which
the instructions are
carried out by your
computer. This is
precisely what logic is all
about.

http://www.pes.edu

You might also like