Академический Документы
Профессиональный Документы
Культура Документы
DATA TYPES
USER
PRIMARY
PRIMARY DERIVED
DERIVED EMPTY
DEFINED
User-defined :
structures , unions
Empty :
void
Prof. Shylaja S S, Head, ISE, PESIT 4
DATA TYPES (CONTD.) :
QUALIFIERS
SIGN
SIZE
ARITHMETIC
ASSIGNMENT
Increment/Decrement
RELATIONAL
LOGICAL
CONDITIONAL
BITWISE
SPECIAL
Prof. Shylaja S S, Head, ISE, PESIT 8
OPERATORS (CONTD.):
•Arithmetic: + , - , / , * . %
•Assignment : =,{+= , *= etc ->shorthand operators)}
•Increment/Decrement : ++ , --
•Relational : < , <= , > , >= , == , !=
•Logical : ! , && , ||
•Conditional : ?
•Bitwise : ~ , << , >> , & , ^, |
•Special operators : ,(comma) , sizeof , & (address
operator)
Prof. Shylaja S S, Head, ISE, PESIT 9
OPERATORS (CONTD.):
IMPORTANT : PRECEDENCE AND
ASSOCIATIVITY
Simple examples to demonstrate the above :
if a=2 , b=5 , c=3 , m=8
1. a+b+c = 10
Precedence of * operator
Thus , (a+(b*c)) = (2+(5*3)) = (2+15) = 17
3. d= a >b ? c > m? a : b : c
7 , 7 , 6 ,6 OR 7 , 8 , 6 ,6
(R to L execution) (PRECEDENCE OF
UNARY(++) OPERATOR )
NOTE : COMPILER DEPENDENT
Prof. Shylaja S S, Head, ISE, PESIT 12
ARRAY :
KEY POINTS :-
Set of similar elements
Homogenous
Continuous memory
Derived dataProf.type
Shylaja S S, Head, ISE, PESIT 13
ARRAY (contd .):
TYPES
1-DIMENSIONAL 2-DIMENSIONAL
SYNTAX :
datatype arrayname[size];
•Size should be a constant
•For eg : int arr[10];Prof. Shylaja S S, Head, ISE, PESIT 14
STRUCTURES AND UNIONS :
SIMILARITY :
1.User-defined
2.Collection of related items
3.Heterogenous in nature
DIFFERENCES :
1.Memory allocation for variable
2.Memory access
Prof. Shylaja S S, Head, ISE, PESIT 15
INPUT AND OUTPUT :
DATA FLOW
UNFORMATTED FUNCTIONS :
•gets
•puts
Prof. Shylaja S S, Head, ISE, PESIT 17
INPUT AND OUTPUT (contd.):
IMPORTANT CONCEPT :
Width specification ( %wd)
Mainly used for beautification of output
Example : int i=284;
printf(“%d , %2d , %3d”,i);
Output : 284,284,284
Excercise :
See the difference in the output when you
execute the following 2 codes :
3.Readability
4.Memory
Prof. Shylaja S S, Head, ISE, PESIT 20
STORAGE CLASSES :
Talk about the SCOPE (who can access) &
LIFETIME (how long it is alive) of a VARIABLE
TYPES
1.
void fn (int n)
Output :
{ n++; }
5
void main()
5
{ auto int i =5;
fn(i);
printf(“%d\n”,i);
fn(i);
printf(“%d”,i);
}
Prof. Shylaja S S, Head, ISE, PESIT 23
STORAGE CLASSES (Contd.):
2.
void fn (int n) Output :
{ n++; } 6
7
void main()
{ static int i =5;
fn(i);
printf(“%d\n”,i);
fn(i);
printf(“%d”,i);
}
int *p; p
printf(“%d\n”,i); 5
printf(“%d\n”,*p); 5
*p =8;
i 8 1000
Prof. Shylaja S S, Head, ISE, PESIT 26
POINTERS (contd.) :
Program to illustrate importance of pointers :
To swap contents of two variables using functions
Method 1 : GLOBAL VARIABLES
int a=2,b=3; OUTPUT :
void swap( ); After swapping :
void main( )
a=3 , b =2
{ swap( );
printf(“After swapping :\n a=%d , b=%d”,a , b);
}
void swap( ) { int t;
t=a ; a =b ; b = t;}
Prof. Shylaja S S, Head, ISE, PESIT 27
POINTERS (contd.) :
METHOD 2 : PASS BY VALUE
FOR EG :
float *p;
i=*p;
// FETCH 4 BYTES OF DATA AND ASSIGN TO i