5. Macros With Arguments
# define PI 3.14
# define AREA ( x ) PI * x * x
main( )
{
float r, a ;
scanf ( ”%f”, r ) ;
a = AREA ( r ) ;
printf ( ”n%f”, a ) ;
a = area ( r ) ;
}f
Order is
unimportant
float area ( float rr ) ;
loat area ( float rr )
{
Order is
unimportant
Macros - Faster
Functions - Less Space
return ( PI * rr * rr ) ;
}
aa == 33..1144 ** rr ** rr ;;
Macros - Faster
Functions - Less Space
6. Macros With Arguments
# define S ( x ) x * x
main( )
{
Alt F Dos Shell
C CPP PR1.C
C exit
Alt F Dos Shell
C CPP PR1.C
C exit
int i, j, k, l, x, n = 2 ;
i = S ( 4 ) ;
j = S ( 2 + 2 ) ;
k = S ( 3 + 1 ) ;
l = S ( 1 + 3 ) ;
m = S ( ++n ) ;
printf ( ”%d %d %d %d %d %d”, i, j, k, l, n, x ) ;
}
jj == 22 ++ 22 ** 22 ++ 22 ;;
kk == 33 ++ 11 ** 33 ++ 11 ;;
ii == 11 ++ 33 ** 11 ++ 33 ;;
ii == 44 ** 44 ;;
mm == ++++nn ** ++++nn ;;
1166 88 77 77 44 1166
11. How much C
Data Types
int
char
float
long
double
short
signed
unsigned
near
far
10 %?
Control Instruc.
if
else
for
while
do
break
continue
switch
case
default
Storage Classes
auto
register
static
extern
Control Instruc.
if
30 %? else
for
20 % ?
while
do
break
continue
switch
case
default
Storage Classes
auto
register
static
extern
goto
return
void
Data Types
int
char
float
long
double
short
signed
unsigned
near
far
2277 // 3322
12. Arrays
main( )
{
int i ;
printf ( ”Enter Marks” ) ;
scanf ( ”%d %d %d”, m1, m2, m3 ) ;
per = ( m1 + m2 + m3 ) / 3 ;
printf ( ”%d”, per ) ;
}
int m1, m2, m3, per ;
for ( i = 1 ; i = 10 ; i++ )
{
}
printf ( ”%d”, per ) ;
13. Choices Available
Use 10 variables each holding 1 value
Use 1 variable holding all 10 values
Array
What is an Array?
Array is a variable capable of
holding more than 1 value at a time
14. Nothing Different
per = { 32, 62, 65, 42, 48, 70, 80, 86, 92, 68 }
per3 per1 per6 per10
In General peri
subscript
H2O
superscript
Screen
per peri
i
ppeerr (( ii )) ppeerr [[ ii ]]
H2O
15. main( )
{
int i ;
Array?
0 9
0
0
printf ( ”Enter Marks” ) ;
scanf ( ”%d %d %d”, m1, m2, m3 ) ;
per[ i ] = ( m1 + m2 + m3 ) / 3 ;
}
int m1, m2, m3, per[ 10 ] ;
for ( i = 1 ; i = 10 ; i++ )
{
}
0 9
for ( i = 1 ; i = 10 ; i++ )
printf ( ”%d”, per[ i ] ) ;
A
Screen
16. Initializing Arrays
main( ) int i = 2 ;
{
optional
int a[ ] = { 7, 6, 11, -2, 26 } ;
int b[ 10 ] ; compulsory
int c[ 1 0 ] = { 16, 13, -8, -7, 25 } ;
printf ( ”%d%d”, sizeof ( a ), sizeof ( b ) ) ;
printf ( ”%d%d”, a[ 0 ], b[ 0 ] ) ;
scanf ( ”%d%d%d”, c [ 7 ] , c [ 8 ] , c [ 9 ] ) ;
c[ 5 ] = 3 + 7 % 2 ;
c[ 6 ] = c[ 1 ] + c[ 3 ] / 16 ;
}
10 20
int i ;
i = 2 ;
7 G
17. Moral
Arrays can be initialized
Array elements can be scanned
Array elements can be calculated
Arithmetic on array elements is allowed
Then how are they different?
18. Storage
j
400
main( )
{
int i = 3, j = 20, k = -5, l = 7, m = 11 ;
int a[ ] = { 3, 20, -5, 7, 11 } ; int ii ;
printf ( ”%u %u %u %u %u”, i, j, k, l, m ) ;
for ( ii = 0 ; ii = 4 ; ii++ )
printf ( ”%u”, a[ ii ] ) ;
502 504 506 508 510
a[0] a[1] a[2] a[3] a[4]
3 20 -5 7 11
502 504 506 508 510
100
}
m
600
400 500 700 600
3 i
100
20
k
-5
500
7 l
700
11
a[ ] = { 2, 1.4, ’A’, 6 } ;
int
- Adjacency
- Similarity
19. Bounds Checking
main( )
{
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
3 60 -5 7 11
500 502 504 506 508
int a[ ] = { 3, 60, -5, 7, 11 } ;
int i ;
for ( i = 0 ; i = 4 ; i++ )
0
0
printf ( ”%d”, a [ i ] ) ;
}
a[ i ] = a[ i ] * 2 ;
for ( i = 0 ; i = 4 ; i++ )
510 512
Subscript out
of range
20. So ...
Arrays are variables capable of storing
multiple values
Array elements are stored in adjacent
memory locations
Checking the bounds of an array is
programmer’s responsibility
21. main( ) Selection Sort
{
int a[ ] = { 17, 6, 13,12, 2 } ;
int i, j, t ;
for ( i = 0 ; i = 3 ; i++ )
{
for ( j = i +
1 ; j = 4 ; j++ )
{
if ( a[ i ] a[ j ] )
{
t = a [ i ] ; a[ i ] = a[ j ] ;
a[ j ] = t ;
}
}
}
for ( i = 0 ; i = 4 ; i++ )
printf ( ”%d”, a[ i ] ) ;
}
j
17 6 13 12 2 i
6 17 13 12 2 0 - 1
6 17 13 12 2
0 - 2
6 17 13 12 2
0 - 3
2 17 13 12 6
0 - 4
2 13 17 12 6
1 - 2
2
12 17 13 6 1 - 3
2
6 17 13 12 1 - 4
2 6
13 17 12
2 6
12 17 13
2 6 12
2 - 3
2 - 4
13 17 3 - 4
22. Bubble Sort main( )
{
int a[ ] = { 17, 6, 13, 12, 2 } ;
int i, j, t ;
for ( j = 0 ; j = 3 ; j++ )
{
- j
for ( i = 0 ; i = 3 ; i++ )
{
if ( a[ i ] a[ i + 1 ] )
{
t =a[ i ] ; a[ i ] = a[ i + 1 ] ;
a[ i + 1 ] = t ;
}
}
}
for ( i = 0 ; i = 4 ; i++ )
printf ( ”%d”, a[ i ] ) ;
}
17 6 13 12 2 i
6 17 13 12 2 0 - 1
i+1
6 13 17 12 2
1 - 2
6 13 12 17 2
2 - 3
6 13 12 2 17
3 - 4
6 13 12 2 0 - 1
6 12 13 2
6 12 2 13
6 12 2
6 2 12
2 6
1 - 2
2 - 3
17
17
17
13 17 0 - 1
1 - 2
13 17
12 13 17 0 - 1
24. main( )
{
Quick Sort At Work
int a[ ] = {17, 6, 13, 12, 2 } ;
qsort ( a, 5, sizeof ( int ), fun ) ;
for ( i = 0 ; i = 4 ; i++ )
printf ( ”n%d”, a[i] ) ;
}
fun ( int *i, int *j )
{
return ( *i - *j ) ;
}
int i ;
int fun ( int *, int * ) ;
Comparison
Function