SlideShare une entreprise Scribd logo
1  sur  25
Macro Expansion 
# define LOWER 1 
main( ) 
{ 
# define UPPER 10 
Macro Expansion 
Macro Template 
int i ; 
for ( i = LOWER ; i <= UPPER ; i++ ) 
printf ( ”n%d”, i ) ; 
} 
for ( i = for ( i = 11 ;; ii <<== 1100 ;; ii++++ ))
Macro Expansion 
# define PI 3.14 
main( ) 
{ 
33..114411552288 
float r, a ; 
printf ( ”Enter radius” ) ; 
scanf ( ”%f”, &r ) ; 
a = PI * r * r ; 
printf ( ”n%f”, a ) ; 
} 
aa == 33..1144 ** rr ** rr ;; 
 
# define PI 3.14 
PI = 6.28 ; 
# define PI 3.14 
PI = 6.28 ; 
float PI = 3.14 ; 
PI = 6.28 ; 
float PI = 3.14 ; 
PI = 6.28 ;
Don’t Remember Constants 
Plank’s Constant 
6.634 * ….. 6.023 * ….. 
Avogadro’s Number 
# define PLANK 6.634E-24 
main( ) 
{ 
a = PLANK * … ; 
b = PLANK / …. ; 
c = PLANK + … ; 
d = PLAN - …. ; 
} 
Error 
Detected
Types Of Macros 
Macros With 
Arguments 
Simple Macros
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
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
Conditional Compilation 
main( ) 
{ 
scanf ( .. ) ; /* input */ 
.. 
.. /* formula */ 
.. 
} 
.. 
.. 
.. 
/* 
*/
Conditional Compilation 
main( ) 
{ 
2 Solutions : 
- # define YES 
- Delete # ifdef, # endif 
scanf ( .. ) ; /* input */ 
.. 
.. /* formula */ 
.. 
} 
.. 
.. 
.. 
# ifdef YES 
# endif 
2 Solutions : 
- # define YES 
- Delete # ifdef, # endif
Miscellaneous Directives 
# define YES 10 main( ) 
{ 
a = YES - .. ; 
printf ( ”YES” ) ; 
}f 
( ) 
{ 
int YES ; 
} 
a = YES + .. ; 
# define YES 20 
# undef YES 
Redefinition 
OK 
Never 
Replaced
# pragma 
# pragma inline 
main( ) 
{ 
asm stc 
asm pushf 
asm pop flags 
.. 
} 
.. 
.. 
.. 
.. 
F7, F8, F9, Ctrl F9 
Won’t Work
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
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 ) ;
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
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
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
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
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?
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
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
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
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
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
Sorting Procedures 
 Selection Sort 
 Bubble Sort 
 Shell Sort 
 Shuttle Sort 
 Heap Sort 
 Merge Sort 
 Radix Sort 
 Quick Sort  
qsort( ) 
Recursion 
Fastest? 
n2 
log2n
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
Chapter 7 
Pg 209-212 
244-249 (C) 
Chapter 7 
Pg 209-212 
244-249 (C) 
CChhaapptteerr 77

Contenu connexe

Tendances

Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd StudyChris Ohk
 
LLVM Backend の紹介
LLVM Backend の紹介LLVM Backend の紹介
LLVM Backend の紹介Akira Maruoka
 
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019corehard_by
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th StudyChris Ohk
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphismmohamed sikander
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd StudyChris Ohk
 
C++ Programming - 14th Study
C++ Programming - 14th StudyC++ Programming - 14th Study
C++ Programming - 14th StudyChris Ohk
 
Being functional in PHP (PHPDay Italy 2016)
Being functional in PHP (PHPDay Italy 2016)Being functional in PHP (PHPDay Italy 2016)
Being functional in PHP (PHPDay Italy 2016)David de Boer
 
2. Базовый синтаксис Java
2. Базовый синтаксис Java2. Базовый синтаксис Java
2. Базовый синтаксис JavaDEVTYPE
 
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!..."A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...akaptur
 

Tendances (20)

Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd Study
 
LLVM Backend の紹介
LLVM Backend の紹介LLVM Backend の紹介
LLVM Backend の紹介
 
C questions
C questionsC questions
C questions
 
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
 
Static and const members
Static and const membersStatic and const members
Static and const members
 
Function basics
Function basicsFunction basics
Function basics
 
Arrays
ArraysArrays
Arrays
 
Vcs23
Vcs23Vcs23
Vcs23
 
Container adapters
Container adaptersContainer adapters
Container adapters
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
C++ Programming - 14th Study
C++ Programming - 14th StudyC++ Programming - 14th Study
C++ Programming - 14th Study
 
Being functional in PHP (PHPDay Italy 2016)
Being functional in PHP (PHPDay Italy 2016)Being functional in PHP (PHPDay Italy 2016)
Being functional in PHP (PHPDay Italy 2016)
 
2. Базовый синтаксис Java
2. Базовый синтаксис Java2. Базовый синтаксис Java
2. Базовый синтаксис Java
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!..."A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
 
C programs
C programsC programs
C programs
 

En vedette (17)

Vcs20
Vcs20Vcs20
Vcs20
 
Vcs14
Vcs14Vcs14
Vcs14
 
Vcs5
Vcs5Vcs5
Vcs5
 
Vcs28
Vcs28Vcs28
Vcs28
 
Vcs24
Vcs24Vcs24
Vcs24
 
Vcs21
Vcs21Vcs21
Vcs21
 
Vcs6
Vcs6Vcs6
Vcs6
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDYC LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
 
ELEMENTARY DATASTRUCTURES
ELEMENTARY DATASTRUCTURESELEMENTARY DATASTRUCTURES
ELEMENTARY DATASTRUCTURES
 
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDYDATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
 
C language unit-1
C language unit-1C language unit-1
C language unit-1
 
Vcs19
Vcs19Vcs19
Vcs19
 
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
C notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit orderC notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit order
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
 
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDYAVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
 
C LANGUAGE NOTES
C LANGUAGE NOTESC LANGUAGE NOTES
C LANGUAGE NOTES
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
 

Similaire à Vcs16

C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2goMoriyoshi Koizumi
 
Sorting programs
Sorting programsSorting programs
Sorting programsVarun Garg
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solutionAzhar Javed
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointersvinay arora
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020vrgokila
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxhappycocoman
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語ikdysfm
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...GkhanGirgin3
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...ssuserd6b1fd
 

Similaire à Vcs16 (20)

C arrays
C arraysC arrays
C arrays
 
pointers 1
pointers 1pointers 1
pointers 1
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
Vcs17
Vcs17Vcs17
Vcs17
 
week-5x
week-5xweek-5x
week-5x
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
Python 1 liners
Python 1 linersPython 1 liners
Python 1 liners
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
 
array
arrayarray
array
 
Blocks+gcd入門
Blocks+gcd入門Blocks+gcd入門
Blocks+gcd入門
 
C programs
C programsC programs
C programs
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 

Plus de Malikireddy Bramhananda Reddy (13)

M v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notesM v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notes
 
B-TREE PREPARED BY M V BRAHMANANDA REDDY
B-TREE PREPARED BY M V BRAHMANANDA REDDYB-TREE PREPARED BY M V BRAHMANANDA REDDY
B-TREE PREPARED BY M V BRAHMANANDA REDDY
 
DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1
 
Data representation UNIT-1
Data representation UNIT-1Data representation UNIT-1
Data representation UNIT-1
 
C SLIDES PREPARED BY M V B REDDY
C SLIDES PREPARED BY  M V B REDDYC SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY M V B REDDY
 
C AND DATASTRUCTURES PREPARED BY M V B REDDY
C AND DATASTRUCTURES PREPARED BY M V B REDDYC AND DATASTRUCTURES PREPARED BY M V B REDDY
C AND DATASTRUCTURES PREPARED BY M V B REDDY
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
Vcs29
Vcs29Vcs29
Vcs29
 
Vcs26
Vcs26Vcs26
Vcs26
 
Vcs22
Vcs22Vcs22
Vcs22
 
Vcs15
Vcs15Vcs15
Vcs15
 
Vcs12
Vcs12Vcs12
Vcs12
 
Vcs8
Vcs8Vcs8
Vcs8
 

Vcs16

  • 1. Macro Expansion # define LOWER 1 main( ) { # define UPPER 10 Macro Expansion Macro Template int i ; for ( i = LOWER ; i <= UPPER ; i++ ) printf ( ”n%d”, i ) ; } for ( i = for ( i = 11 ;; ii <<== 1100 ;; ii++++ ))
  • 2. Macro Expansion # define PI 3.14 main( ) { 33..114411552288 float r, a ; printf ( ”Enter radius” ) ; scanf ( ”%f”, &r ) ; a = PI * r * r ; printf ( ”n%f”, a ) ; } aa == 33..1144 ** rr ** rr ;; # define PI 3.14 PI = 6.28 ; # define PI 3.14 PI = 6.28 ; float PI = 3.14 ; PI = 6.28 ; float PI = 3.14 ; PI = 6.28 ;
  • 3. Don’t Remember Constants Plank’s Constant 6.634 * ….. 6.023 * ….. Avogadro’s Number # define PLANK 6.634E-24 main( ) { a = PLANK * … ; b = PLANK / …. ; c = PLANK + … ; d = PLAN - …. ; } Error Detected
  • 4. Types Of Macros Macros With Arguments Simple Macros
  • 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
  • 7. Conditional Compilation main( ) { scanf ( .. ) ; /* input */ .. .. /* formula */ .. } .. .. .. /* */
  • 8. Conditional Compilation main( ) { 2 Solutions : - # define YES - Delete # ifdef, # endif scanf ( .. ) ; /* input */ .. .. /* formula */ .. } .. .. .. # ifdef YES # endif 2 Solutions : - # define YES - Delete # ifdef, # endif
  • 9. Miscellaneous Directives # define YES 10 main( ) { a = YES - .. ; printf ( ”YES” ) ; }f ( ) { int YES ; } a = YES + .. ; # define YES 20 # undef YES Redefinition OK Never Replaced
  • 10. # pragma # pragma inline main( ) { asm stc asm pushf asm pop flags .. } .. .. .. .. F7, F8, F9, Ctrl F9 Won’t Work
  • 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
  • 23. Sorting Procedures Selection Sort Bubble Sort Shell Sort Shuttle Sort Heap Sort Merge Sort Radix Sort Quick Sort qsort( ) Recursion Fastest? n2 log2n
  • 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
  • 25. Chapter 7 Pg 209-212 244-249 (C) Chapter 7 Pg 209-212 244-249 (C) CChhaapptteerr 77