Вы находитесь на странице: 1из 33

Prof.

Shylaja S S, Head, ISE, PESIT 1


DATA TYPES:
 Definition : The type of the data that a
variable can store in the memory can be
defined using data types.
 Significance :
1. Memory allocation is done according to the
data type
2. Permissible operations
3. Range of data

Prof. Shylaja S S, Head, ISE, PESIT 2


DATA TYPES (CONTD.) :

DATA TYPES

USER
PRIMARY
PRIMARY DERIVED
DERIVED EMPTY
DEFINED

Prof. Shylaja S S, Head, ISE, PESIT 3


DATA TYPES (CONTD.) :
Primary type :
int , float , double, char

Derived type : (derived from primary)


arrays , pointers

User-defined :
structures , unions

Empty :
void
Prof. Shylaja S S, Head, ISE, PESIT 4
DATA TYPES (CONTD.) :
QUALIFIERS

SIGN

SIZE

SIGN QUALIFIERS : Signed , unsigned (for primary


data types)

SIZE QUALIFIERS : Short , long


Prof. Shylaja S S, Head, ISE, PESIT 5
DATA TYPES (CONTD.) :
PRACTICE PROGRAM : To add 2 numbers
CASE 1 :
#include<stdio.h>
If input is 123 , 24
void main( )
Output : 147
{
CASE 2 :
int a , b , sum ;
If input is
printf(“Enter 2 numbers \n “); 123567+24111
scanf(“%d %d “ ,&a , &b); Program does not
sum = a +b ; work as input is out
of the range for
printf(“Sum = %d” , sum ); integer data type
}
Prof. Shylaja S S, Head, ISE, PESIT 6
VARIABLES :
 Definition : Identifier whose value can be
changed during program execution
 Remember :
1. Important to choose name of variable
appropriately.(useful in long programs)
2.Choose variable name as per the rules
defined

Prof. Shylaja S S, Head, ISE, PESIT 7


OPERATORS :
TYPES

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

L to R associativity for same operator


Thus , ((a+b)+c) = ((2+5)+3) = (7+3) = 10

Prof. Shylaja S S, Head, ISE, PESIT 10


OPERATORS (CONTD.):
2 . a+b*c =17

Precedence of * operator
Thus , (a+(b*c)) = (2+(5*3)) = (2+15) = 17

3. d= a >b ? c > m? a : b : c

R to L associativity for conditional operator


Thus , d = (a>b?(c>m?a:b):c)
= (2>5?(3>8?2:5):3)
= (2>5?5:3)
d=3 Prof. Shylaja S S, Head, ISE, PESIT 11
OPERATORS (CONTD.):
WHAT IS THE OUTPUT ??????
i=5;
printf (“ %d , %d , %d , %d” , i++ , i , i++ , ++i );

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

INPUT OPERATION OUTPUT OPERATION

(Data flows from (Data flows from


the external the program to
device into the the external
program) device)
Prof. Shylaja S S, Head, ISE, PESIT 16
INPUT AND OUTPUT (contd.):
Formatted functions (use format specifiers)

scanf (Input function)

printf (Output function)

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 :

for (i=1 ; i<=12 ; i++) for (i=1 ; i<=12 ; i++)


printf ( “%d” , i ); printf ( “%2d” , i );
Prof. Shylaja S S, Head, ISE, PESIT 18
INPUT AND OUTPUT (contd.):

WILL THE PROGRAM WORK ?????


void main( )
{
char *p;
gets ( p ) ;
puts ( p );
}

Prof. Shylaja S S, Head, ISE, PESIT 19


FUNCTIONS :
A subprogram/module which performs a
specific task
 ADVANTAGES :  DISADVANTAGES :
1.Debugging
Execution time is
2.Reusability more

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

AUTO STATIC EXTERN REGISTER

Prof. Shylaja S S, Head, ISE, PESIT 21


STORAGE CLASSES (Contd.):
KEY POINTS :
 Auto local and static local have a function scope
 Register mainly used for faster access of the
variable
 Extern declaration more important across files
 Auto global variables can be used in more than
one file using extern declaration
 Static global variables can be accessed only by
functions in the file
Prof. Shylaja S S, Head, ISE, PESIT 22
STORAGE CLASSES (Contd.):
Programs to understand key concepts of storage classes :

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);
}

Prof. Shylaja S S, Head, ISE, PESIT 24


STORAGE CLASSES (Contd.):
Program to illustrate importance of extern declaration
void main( ) void main( )
int i; //global
{ {
void main( ) printf(“ %d ” , i ) ; extern int i;
} //declaration
{
printf(“ %d ” , i) ;
printf(“ %d ” , i ) ; int i; //global only to f1,f2 }
}
void f1( ) int i; //definition
void f1( ) { i++; }
void f2( ) void f1( )
{ i++; }
{ i++; } { i++; }
void f2( ) void f2( )
{ i++; } { i++; }
Thus,undefined symbol i
OUTPUT : 0 (error)
Prof. Shylaja S S, Head, ISE, PESIT 25
POINTERS :
 A pointer variable stores address of
another variable
i
int i =5; 5 1000

int *p; p

p=&i; 1000 2000

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

void swap (int , int );


void main( )
Output :
{ int a=2,b=3; After swapping:
swap(a,b);
printf (“After swapping :\n a=%d , b=%d”,a , b); a=2 , b =3
}
void swap (int a , int b)
{
int t;
t=a ;
a =b ;
b = t;
}

Prof. Shylaja S S, Head, ISE, PESIT 28


POINTERS (contd.) :
METHOD 3 : PASS BY REFERENCE

void swap (int *, int * );


void main( )
Output :
{ int a=2,b=3; After swapping:
swap(&a,&b);
printf (“After swapping :\n a=%d , b=%d”,a , b); a=3 , b =2
}
void swap (int *i , int *j)
{
int t;
t=*i ;
*i =*j ;
*j =t;
}

Prof. Shylaja S S, Head, ISE, PESIT 29


POINTERS (contd.) :
1. To retreive / access
Why do we have
pointers to value
2. Pointer arithmetic
int , float etc ?????

FOR EG :
float *p;
i=*p;
// FETCH 4 BYTES OF DATA AND ASSIGN TO i

Prof. Shylaja S S, Head, ISE, PESIT 30


POINTERS (contd.) :
ARRAYS AND POINTERS : ‘Arrays are constant pointers’
Example program:
void main( )
{ OUTPUT :
int a[3] = {5 , 6, 7 } ;
f1( a , 3 ) ;
5
// a++ ; (array constant , thus , statement is incorrect)
6
}
void f1 (int *p , int n )
{ printf( “%d\n”,*p) ;
p++;
printf( “%d\n”,*p) ;
}
Prof. Shylaja S S, Head, ISE, PESIT 31
POINTERS (contd.) :
PRACTICE PROGRAM : To read one student’s marks and add a
grace of 5 marks using pass by reference
void main( )
{ Input : 85
float m; Output :
scanf(“ % d ”,&m) ; 90
grace(&m);
printf(“ % d “ ,m); EXCERCISE :
} REPEAT THE SAME
void grace (float *p) PROCEDURE FOR
MANY STUDENTS
{ *p = *p + 5; }

Prof. Shylaja S S, Head, ISE, PESIT 32


Prof. Shylaja S S, Head, ISE, PESIT 33

Вам также может понравиться