SlideShare a Scribd company logo
1 of 578
Download to read offline
Why C? 
 C is simple 
 C is small 
 C is fast 
 C offers better interaction with 
hardware
Introduction
Historical Development of C 
1960 International Committee 
1963 National Committee 
1967 1 Man Committee 
ALGOL 60 
CPL 
Martin Richards - BCPL 
1970 Ken Thompson, AT & T Bells Lab 
1972 Dennis Ritchie, AT & T Bells Lab 
B 
C
Where C Stands 
Computer Languages 
LLL HLL 
Machine Oriented Problem Oriented 
Better Machine 
efficiency 
Better Programming 
efficiency 
Ex. Assembly, 
Machine 
Ex. Basic, Fortran, 
Pascal,Cobol
In The Beginning... 
English C 
Alphabets, Digits 
Words, Numbers 
Sentences 
Paragraph 
Alphabets, Digits, Sp. 
Symbols 
Constants, Variables, 
Keywords 
Statements or 
Instructions 
Program
Alphabets, Digits, 
Special Symbols 
Alphabets 
A - Z, a - z 
Digits 
0 - 9 
Sp. Symbols 
Total - 32
Constants and Variables 
Variables 
3 x + 2 y = 20 
Constants
C Constants 
Primary Secondary 
Pointer 
Integer 
Real 
Character 
Array 
String 
Structure 
Union 
Enum 
etc.
Integer Constants 
Ex. 421 -62 +45 4096 
Rules: 
 
 
1. No decimal point. 72 72.0 
2. May be +ve or -ve. Default: +ve 
32,500 4 7 3 
  
3. No comma or 
spaces 
4. Valid Range: -32768 to +32767
Real Constants 
Ex. 427.62 +24.297 -0.00254 
Rules: 
1. Must contain a decimal point. 
2. May be +ve or -ve. Default: +ve 
3. No comma or spaces. 
4. Valid Range: -3.4x 1038 to +3.4 x 1038
Forms of Real Constants 
427.62 
+24.295 
-0.00254 
Fractional Form 
4.2762E2 
2.4295e1 
-2.54e-3 
Exponential Form 
E 
4.2762 2 
Mantissa Exponent
Character Constants 
Ex. ’A’ ’m’ ’3’ ’+’ 
Rule : A single character enclosed 
within a pair of ’ ’. 
Are They OK? 
‘Z’ 
’Nagpur’ 

C Variables 
Primary Secondary 
Pointer 
Array 
String 
Structure 
Union 
Enum 
etc. 
Integer 
Real 
Character
Variables 
How many types? 
As many as the types of constants. 
 Is it necessary to identify types? 
Yes. 
Why? 
Why?
What Happens in Memory... 
y x 
z 
x = 3 
Memory 
4 3 
7 
y = 4 
z = x + y
So A Variable is... 
 An entity whose value can 
change 
 A name given to a location in 
memory
How to Identify Types 
3 
3.0 
’3’ 
a 
b 
c 
Integer Constant 
Real Constant 
Character Constant 
? 
? 
?
Different Languages, 
Different Rules 
BASIC FORTRAN 
i...n - Integer 
a - h, o - z - Real 
a% - Integer 
b! - Real 
c$ - Character
C’s Way of Identifying 
Variables 
inta floatb char c 
a = 3 b = 3.0 c = ’3’
Rules for Building 
Variable Names 
 First character must be an alphabet, rest 
can be alphabets, digits, or underscores. 
Ex. pop98, si_int 
_ - 
si-int 
si_int 
 Length <= 8 (Usually) 
 No commas or spaces 
 Variable names are case sensitive 
Ex. abc ABC Abc aBc AbC
C Keywords 
 How many? 
 What are Keywords? 
 What are Reserved 
words?
Would This Work? 
integer a 
real b 
character c 
 
 
 
And How About This... 
int float float char 
float = 3 char = 3.14
Where Do We Stand? 
C 
Alphabets, Digits, Sp. Symbols 
Constants, Variables, Keywords 
Statements or Instructions 
Program
The First C Program 
p = 1000.50 
n = 3 
r = 15.5 
si = p * n * r / 100
Declaring Variables... 
float p, r, si 
int n 
p = 1000.50 
n = 3 
r = 15. 5 
si = p * n * r / 100 
Tip1: All variables must be declared.
Printing Values... 
float p, r, si 
int n 
p = 1000.50 
n = 3 
r = 15. 5 
si = p * n * r / 100 
printf ( ”%f”, si )
Terminology Matters 
( ) Parentheses 
{ } Braces 
[ ] Brackets
General Form of printf( ) 
printf ( ”format string”, list of variables ) 
can contain 
%i - integer 
%f - float 
%c - character 
Ex. printf ( ”%f %f %f %i”, p, r, si, n )
Statement Terminators 
I am a boy 
I go to school 
float p, r, si ; 
int n ; 
float p, r, si 
int n 
Statement Terminator 
: colon ; semicolon
Free Form Language 
float p, r, si ; 
int n ; 
is same as 
float p, r, si ; int n ;
What To Execute 
main( ) 
Collective 
Name 
float p, r, si ; 
int n ; 
p = 1000.50 ; 
n = 3 ; 
r = 15. 5 ; 
si = p * n * r / 100 ; 
printf ( ”%f”, si ) ;
What Belongs to main( ) 
main( ) 
{ 
float p, r, si ; 
int n ; 
p = 1000.50 ; n = 3 ; r = 15. 5 ; 
si = p * n * r / 100 ; 
printf ( ”%f”, si ) ; 
}
Comments Are Useful 
/* Calculation of simple interest */ 
main( ) 
{ 
float p, r, si ; 
int n ; 
p = 1000.50 ; n = 3 ; r = 15. 5 ; 
si = p * n * r / 100 ; 
printf ( ”%f”, si ) ; 
} 
Note Remark
Tips About Comments 
 Any number of comments anywhere 
 
 Multiline comments - 
/* ............................. 
............................... */ 
 Nested comments -  
/* .............. /* ....... */ ........ */
A More General Program 
/* Calculation of simple interest */ 
main( ) 
{ 
float p, r, si ; int n ; 
1000.50 3 15 
printf ( ”Enter values of p, n and r : ” ) ; 
scanf ( ”%f %i %f”, & p, & n, & 
r ) ; 
si = p * n * r / 100 ; 
‘Address of ’ 
printf ( ”%f”, si ) ; 
operator 
}
Try Your Hand At... 
Page No. 34 - 35 
[G] (a) to (f)
C Compilers 
 Turbo C 
 Quick C 
 Microsoft C 
 Aztech C 
 Zortech C 
 Lattice C 
 Watcom C 
 GreenLeaf C 
 Vitamin C 
All Provide 
an IDE 

Integrated Development 
Environment 
Editor 
I D E 
Compiler 
Debugger 
Linker Preprocessor
Editor and Compiler 
 Editor - Helps in typing / editing of a 
program 
 Compiler - Converts C language 
program to machine 
language program
Editing Commands 
Cursor Movement Deletion 
 Up Arrow Del 
 Down Arrow 
 Right Arrow 
 Left Arrow 
Home End 
PgUp PgDn 
Ctrl+Home Ctrl+End 
Ctrl+PgUp Ctrl+PgDn 
Backspace 
Ctrl+T 
Ctrl+Y 
Computers are fun 
Screen 
File
Some More Commands 
File Menu Miscellaneous 
F2 Save 
Ctrl+F9 Compile 
Alt+F5 Output- 
Screen 
New 
Save 
Save As 
Open 
Exit 
Dos Shell 
Permanent 
Temporary 
Tip: Use meaningful filenames. Ex. CH1PR1.C
Interchanging Contents of 
Two Variables 
main( ) 
{ 
printf ( ”Enter values of c and d” ) ; 
c d 
5 10 
c d 
10 10 
c d 
10 10 
int c, d ; 
scanf ( ”%i%i”, &c, &d ) ; 
c = d ; 
d = c ; 
5 10 
printf ( ”%i %i”, c, d ) ; 
} 
10 10
Interchanging Contents of 
Two Variables 
main( ) 
{ 
int c, d , t ; 
printf ( ”Enter values of c and d” ) ; 
c d 
5 10 
t 
x 
c d 
5 10 
t 
5 
c d 
10 10 
t 
5 
c d 
10 5 
t 
5 
scanf ( ”%i%i”, &c, &d ) ; 
t = c ; 
c = d ; 
d = t ; 
printf ( ”%i %i”, c, d ) ; 
} 
5 10 
10 5
One More Way 
scanf ( ”%i%i”,&c, &d ) ; 
c d 
25 10 
c d 
25 15 
c d 
10 15 
c = c + d ; 
d = c - d ; 
c = c - d ; 
15 10 
c d 
15 10
Sum of Digits 
main( ) 
{ 
printf ( ”Enter a five digit no.” ) ; 
scanf ( ”%d”, &n) ; 
2 6 9 1 3 
d1 
26913 
d2 d3 d4 d5 
int n ; 
.. .. 
.. .. 
}
Calculations 
26913 / 10 2691 - Division 
26913 % 10 3 - Modular division 
10 26913 2691 
26910 
3 
Mod
The Whole Picture 
main( ) 
{ 
… 
n 
26913 
d5 3 2691 
d4 
d3 
d2 
d1 
1 
9 
6 
2 
269 
26 
2 
2 
d5 = n % 10 ; 
n = n / 10 ; 
d4 = n% 10 ; 
n = n / 10 ; 
d3 = n % 10 ; 
n = n / 10 ; 
d2 = n% 10 ; 
n = n / 10 ; 
d1 = n% 10 ; 
s = d1 + d2 + d3 + d4 + d5 ; 
printf ( ”%i”, s ) ; 
} 
21
Is % ( modulus ) Really 
Useful 
 Leap year or not 
 Odd / Even 
 Prime or not
Where Are We.... 
Alphabets, Digits, Sp. Symbols 
Constants, Variables, Keywords 
Statements or Instructions 
Program
C Statements / Instructions 
 Type Declaration Instruction 
Ex. int i, j, k ; 
float a, b, c ; 
char ch ;
Type Declaration, A few 
Subtleties 
int a ; 
a = 5 ; 
is same as 
int a = 5 ; 
 int a = 5, b = 10, c = a + b * 5% 2 ; 
Here order is important
Is This Ok? 
 int a, b, c, d ; 
a = b = c = d = 5 ;  
 int a = b = c = d = 5 ; 
C Statements/Instructions 
 Type Declaration Instructions 
 Arithmetic Instructions 
Ex. s = d1 + d2 + d3 ; 
si = j * n * r / 100 ; 
c = 5.0 / 9 * f - 32 ; 
+, -, *, /, % - Arithmetic Operators
Arithmetic Instructions, a 
few Small Issues 
 i = j * k + 3 ; 
j * k + 3 = i ; 
 i = 3 ; 
3 = i ; 
 a = b ( c + d ) ; 
a = b * ( c + d ) ; 
 
 
 
 
 
 
Tip: 
Tip:
Type of Arithmetic 
Instructions 
 Integer mode AI 
 Real mode AI 
 Mixed mode AI 
Ex. int a = 3, b = 4, c ; 
c = a * b + 5% 6 - b + 14 ; 
???? + 
- % + * =; 
c a b 5 6 14 
Integer mode AI
Legal Arithmetic Operations 
Operand1 Operand2 Result 
int int 
float float 
int float 
float int 
int 
float 
float 
float
Try This 
int a ; 
a = 5 / 2 ; 
a = 5.0 / 2 ; 
a = 5 / 2.0 ; 
a = 5.0 / 2.0 ; 
a = 2 / 5 ; 
a = 2.0 / 5 ; 
a = 2 / 5.0 ; 
a = 2.0 / 5.0 ; 
22 
2 
2000 
0
Try This 
float a ; 
a = 5 / 2 ; 
a = 5.0 / 2 ; 
a = 5 / 2.0 ; 
a = 5.0 / 2.0 ; 
a = 2 / 5 ; 
a = 2.0 / 5 ; 
a = 2 / 5.0 ; 
a = 2.0 / 5.0 ; 
2.0 
2.5 
2.5 
2.5 
0.0 
0.4 
0.4 
0.4
Which Is Correct? 
float c, f = 212.0 ; 
c = 5 / 9 * ( f - 32 ) ; 
c = 5 / 9.0 * ( f - 32 ) ; 
c = ( f - 32 ) * 5 / 9 ; 
0.0 
100.0 
100.0 
Note: Parenthesis matter
Heirarchy/Priority/Precedence 
c = a + b * c % 5 - d * 6 
1 
2 
4 
Operators 
* / % 
+ - 
= 
6 
3 
5
C Instructions 
 Type declarations Instructions 
 Arithmetic Instructions 
 Input / Output Instructions 
printf( ) - Output 
scanf( ) - Input
General Form of printf( ) 
printf ( ”format string”, list of variables ) ; 
printf ( ”Enter values of c and d” ) ; 
 List of variables is optional. 
printf ( ”%i %i %i”, a, 35, 2 + 6% 3 ) ; 
 List can contain variables, 
constants 
or expressions.
printf( ) 
printf ( ”format string”, list of variables ) ; 
- int - %i , %d 
- float - %f 
- char - %c 
Escape 
sequences 
Any other 
characters 
can contain 
Format specifiers
Numbering Systems 
Binary 
( 0 - 1 ) 
0 
1 
10 
11 
100 
... 
111 
1000 
…. 
…. 
Decimal 
( 0- 9 ) 
0 
1 
. 
9 
10 
11 
.. 
99 
100 
... 
... 
Octal 
( 0- 7 ) 
01. 
7 
10 
11 
.. 
77 
100 
... 
... 
Hexadecimal 
( 0-9, A- F ) 
0 
1 
. 
9 
A 
. 
F 
10 
.. 
FF 
100
Conversions 
Deci 473 decimal 4 * 102+ 7 * 101 + 3 * 100 = 473 
Octal 11 decimal 1 * 81 + 1 * 80 = 9 
Hexa 11 decimal 1 * 161 + 1 * 160 = 17 
Binary 11 decimal 1 * 21 + 1 * 20 = 3
More Conversions 
Dec. 9 Octal 11 
8 9 
8 1 1 
0 1 
Dec. 17 Hex 11 
16 17 
16 1 1 
0 1 
Dec. 3 Binary 11 
2 3 
2 1 1 
0 1
printf( ) Makes It Handy 
 printf ( ”%d %o %x %X”, 10, 10, 10, 10 ) ; 
10 12 a A 
 printf ( ”%d %d %d %d”, 10, 12, a, A ) ; Error 
 printf ( ”%d %d %d %d”, 10, 012, 0xa, 0XA ) ; 
10 10 10 10 
zero or O?
Escape Sequences 
 n - Newline 
 t - Tab 
 printf ( ”%dn%dt%d”, 10, 20, 30 ) ; 
10 
20 30
Any Other Characters 
 printf ( ”Enter values of c and d” ) ; 
 printf ( ”Hello” ) ; 
 printf ( ”nSimple Interest = Rs. %f”, si ) ; 
Escape 
sequence Format 
Any other specifier 
character
C Instructions 
Type Declaration 
Arithmetic Instruction 
Input/Output Instruction 
Control Instructions
Try At Home 
Let Us C Chapter 1 - Page 29-36
C Instructions 
 Type Declaration Instructions 
 Arithmetic Instructions 
 Input / Output Instructions 
 Control Instructions
Control Instructions 
 What are they? 
- Control the sequence of execution 
of instructions 
 What different types? 
- Sequence 
- Decision 
- Repitition 
- Case
Normal C Program 
main( ) 
{ 
int ..... ; float ...... ; 
printf ( ...... ) ; 
scanf ( ...... ) ; 
a = .... ; 
b = .... ; 
printf ( .......) ; 
} 
Tip: Sequence CI is the default CI
Decision Control Instruction 
/* Calculation of total expenses */ 
main( ) 
{ 
int qty ; 
float price ; 
printf ( ”Enter quantity and price” ) ; 
scanf ( ”%d%f”, & qty, &price ) ; 
... 
... 
}
/* Calculation of total expenses */ 
main( ) 
{ 
int qty ; float price ; 
printf ( ”Enter quantity and price” ) ; 
scanf ( ”%d%f”, &qty, &price ) ; 
} 
if ( qty >= 1000 ) 
dis = 10 ; 
int dis ; float totexp ; 
200 15.50 
1200 15.50 
else 
dis = 0 ; 
totexp = qty * price - qty * price * dis / 100 ; 
printf ( ”Total expenses = Rs %f”, totexp ) ;
Tips 
 if, else - Keywords 
 General form: 
if ( condition ) 
statement1 ; 
else 
statement2 ; 
Relational 
Operators 
 Cond. is usually built using <, >, <=, >=, = =, != 
a = b 
a = = b 
Assignment 
Comparison
Would This Work? 
int a = 3, b = 4, c, d ; 
printf ( ”%d”, a + b ) ; 
7 
1 
12 
0 
printf ( ”%d”, a <= b ) ; 
c = a * b ; 
printf ( ”%d”, c ) ; 
d = a = = b ; 
printf ( ”%d”, d ) ; 
Tip: Condition - True - Replaced by 1 
Condition - False - Replaced by 0
Is it Monday or Tuesday 
int a ; 
printf ( ”%d”, a ) ; Garbage 
Tip: Unless specifically initialised a 
variable contains a garbage value.
/* Calculation of Total Expenses */ 
main( ) 
{ 
int qty ; float price ; int dis ; 
printf ( ”Enter quantity and price” ) ; 
scanf ( ”%d%f” , &qty, &price ) ; 
if ( qty >= 1000 ) 
dis = 10 ; 
totexp = qty * price - qty * price * dis / 100; 
Tip: else block is optional 
= 0 float totexp ; 
printf ( ”Total expenses = Rs %f”, totexp ) ; 
}
/* Calculation of gross salary */ 
main( ) 
{ 
float bs, hra, ca, da, gs ; 
printf ( ”Enter basic salary” ) ; 
scanf ( ”%f”, &bs ) ; 
if ( bs >= 1500 ) 
da = bs * 95 / 100 ; 
only this 
hra = bs * 20 / 100 ; 
belongs to 
ca = bs * 12 / 100 ; 
if block 
else 
da = bs * 92 / 100 ; 
only this 
hra = bs * 15 / 100 ; 
belongs to 
ca = 200 ; 
else block 
gs = bs + hra + da + ca ; 
printf ( ”Gross salary = Rs. %f”, gs ) ; 
}
/* Calculation of gross salary */ 
main( ) 
{ 
float bs, hra, ca, da, gs ; 
printf ( ”Enter basic salary” ) ; 
scanf ( ”%f”, &bs ) ; 
if ( bs >= 1500 ) 
{ 
da = … ; hra = ... ; ca = … ; 
} 
else 
{ 
da = … ; hra = ... ; ca = … ; 
} 
gs = bs + da + hra + ca ; 
printf ( ”Gross salary = Rs %f”, gs ) ; 
}
One More Form 
if ( condition ) 
{ 
statement1 ; 
statement2 ; 
} 
else 
{ 
statement3 ; 
statement4 ; 
} 
Tip: - Default scope of if and else is only the 
next statement after them. 
- To override the default scope use { }
Leap Year or Not 
main( ) 
{ 
printf ( ”Enter year” ) ; 
scanf ( ”%d”, &y ) ; 
4 1996 499 
1996 
0 
Leap 
4 2000 500 
2000 
0 
Leap 
4 1900 475 
1900 
0 
Not Leap 
int y ; 
… 
}
main( ) 
{ 
. . . 
if ( y % 100 == 0 ) 
{ 
if ( y % 400 == 0 ) 
printf ( ”Leap” ) ; 
else 
printf ( ”Not Leap” ) ; 
} 
else 
{ 
if ( y % 4 == 0 ) 
printf ( ”Leap” ) ; 
else 
printf ( ”Not Leap” ) ; 
} 
} 
Nested if-else 
statements are legal.
Decide First Day of Any 
Year 
main( ) 
{ 
int y ; 
printf ( ”Enter year” ) ; 
scanf ( ”%d”, &y ) ; 
. . . 
} 
1998
The Logic Behind It 
8/ 1/ 1 Monday 
… 
… 
… 
1/ 1/ 1998 
1 /1 / 1 Monday 
29/ 1 / 1 ? 
1/ 1/ 1 to 28/1 / 1 
28 % 7 
1/ 1/ 1 to 31/ 12/ 1997 
? % 7 
0 
? 
1/ 1/ 1 Monday 
15/ 1/ 1 Monday 
22/ 1/ 1 Monday 
29/ 1/ 1 Monday 
?
main( ) 
{ 
printf ( ”Enter year” ) ; 
scanf ( ”%d”, &y ) ; 
leapdays =( y - 1 ) / 4- ( y - 1 ) / 100 + ( y - 1 ) / 400 ; 
totaldays = normaldays + leapdays ; 
firstday = totaldays % 7 ; 
if ( firstday == 0 ) 
printf ( ”Monday” ) ; 
.. .. 
.. .. 
} 
1/ 1/ 1 to 28/ 1/ 1 
28 % 7 
1/ 1/ 1 to 31/ 12/ 1997 
? % 7 
0 
? 
int y ;, firstday, normaldays, leapdays, totaldays ; 
normaldays = ( y - 1 ) * 365 ;
main( ) 
{ 
int y, firstday, leapdays ; 
long int normaldays, totaldays ; 
.. .. 
.. .. 
normaldays = ( y - 1 ) * 365 ; 
.. .. 
.. .. 
} 
int -32678 to +32767 
long int -2147483648 to +2147783647
What’s Wrong Here? 
normaldays = ( y - 1 ) * 365 ; 
float a ; 
a = 5 / 2 ; 
float a ; 
a = 5 / 2.0 ; 
Soln: normaldays = ( y - 1 ) * 365L ;
The sizeof( ) Operator 
2 4 
int i ; 
long int j ; 
printf ( ”%d %d”, sizeof ( i ), sizeof ( j ) ) ; 
printf ( ”%d %d”, sizeof ( int ), sizeof ( long int )); 
Keyword Operator   
2 4
Are You Sure? 
4 % 3 
3 % 4 
-3 % 4 
3 % -4 
-3 % -4 
1 
3 
-3 
3 
-3 
Tip: Sign of remainder is same as sign of numerator
Try Your Hand At 
Exploring C Chapter 1 
Let Us C Chapter 2 Pg 60 - 64 ( e )
What Will Be The Output 
main( ) 
{ 
int a = 5, b = 10 ; 
if ( a >= 20 ) ; 
b = 30 ; 
30 
printf ( ”%d”, b ) ; 
} 
; - Null statement. Doesn’t do anything 
on execution
What Will Be The Output 
main( ) 
{ 
int a = 10 ; 
if ( a = 5 ) 
printf ( ”Hi” ) ; 
} 
if ( a = 5 ) if ( a ) if ( 5 ) 
Tip : Truth in C is nonzero, whereas, 
falsity is zero.
main( ) 
{ 
if ( per >= 60 ) 
int per ; 
printf ( ”First division” ) ; 
else 
{ 
if ( per >= 50 ) 
printf ( ”Second division” ) ; 
Three 
Problems 
int m1, m2, m3, m4, m5 ; 
printf ( ”Enter marks in five subjects” ) ; 
scanf ( ”n%d%d%d%d%d”, &m1, &m2, &m3, &m4, &m5 ); 
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ; 
else 
{ 
if ( per >= 40 ) 
printf ( ”Third division” ) ; 
else 
printf ( ”Fail” ) ; 
} 
} 
}
main( ) 
{ 
int m1, m2, m3, m4, m5, per ; 
printf ( ”Enter marks in five subjects” ) ; 
scanf ( ”%d%d%d%d%d”, &m1, &m2, &m3, &m4, &m5 ) ; 
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ; 
if ( per >= 60 ) 
printf ( ”First division” ) ; 
if ( per >= 50 ) 
printf ( ”Second division” ) ; 
if ( per >= 40 ) 
printf ( ”Third division” ) ; 
else 
printf ( ”Fail” ) ; 
} 
Anything 
Wrong? 
?
main( ) 
{ 
int m1, m2, m3, m4, m5, per ; 
printf ( ”Enter marks in five subjects” ) ; 
scanf ( ”%d%d%d%d%d”, &m1, &m2, &m3, &m4, &m5 ) ; 
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ; 
if ( per >= 60 ) 
printf ( ”First division” ) ; 
if ( per >= 50 && per < 60 ) 
printf ( ”Second division” ) ; 
if ( per >= 40 && per < 50 ) 
printf ( ”Third division” ) ; 
else 
printf ( ”Fail” ) ; 
} 
Still Anything 
Wrong?
main( ) 
{ 
int m1, m2, m3, m4, m5, per ; 
printf ( ”Enter marks in five subjects” ) ; 
scanf ( ”%d%d%d%d%d”, &m1, &m2, &m3, &m4, &m5 ) ; 
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ; 
if ( per >= 60 ) 
printf ( ”First division” ) ; 
if ( per >= 50 && per < 60 ) 
printf ( ”Second division” ) ; 
if ( per >= 40 && per < 50 ) 
printf ( ”Third division” ) ; 
if ( per < 40 ) 
printf ( ”Fail” ) ; 
} 
Is This 
Better?
Logical Operators 
&& - And 
|| - Or 
! - Not 
Useful in 
Checking 
Ranges 
Yes / No 
Problem
main( ) 
{ 
int age ; 
char s, ms ; 
printf ( ”Enter age, sex, marital status” ) ; 
scanf ( ”%d%c%c”, &age, &s, &ms ) ; 
if ( ms == m ) 
26 m u ’ ’ 
printf ( ”Insured” ) ; 
else 
{ 
if ( s == m ) 
{ 
if ( age > 30 ) 
printf ( ”Insured” ) ; 
else 
printf ( ”Not Insured” ) ; 
} 
else 
{ 
if ( age > 25 ) 
printf ( ”Insured” ) ; 
else 
printf ( ”Not In.” ) ; 
} 
} 
} 
’ ’ 
Not Insured
Program Using Logical 
Operators main( ) 
{ 
int age ; 
char s, ms ; 
printf ( ”Enter age, sex, marital status” ) ; 
scanf ( ”%d%c%c”, &age, &s, &ms ) ; 
} 
26 m u 
if ( ms = = ’m’ ms = = ’u’ s = = ’m’ age > 30 
( ) 
|| 
( ) 
( && && 
) 
ms = = ’u’ s = = ’f’ age > 25 ) 
|| 
printf ( ”Insured” ) ; 
else 
printf ( ”Not Insured” ) ; 
&& && 
Not Insured
Working of && And || 
cond1 cond2 cond1 && cond2 cond1 || cond2 
True True 
True True 
False False 
True False 
False True 
False False 
False 
True 
False 
True
Hierarchy of Operators 
Operators Type 
! Logical Not 
* / % Arithmetic 
+ - Arithmetic 
< > <= >= Relational 
= = != Relational 
&& Logical And 
|| Logical Or 
= Assignment 
Increasing 
Priority 
Other Unary 
Operators - + & sizeof
Exchanging Blocks 
main( ) 
{ 
int a = 3, b = 4; 
if ( a <= b ) 
printf ( ”A”) ; 
else 
printf ( ”B” ) ; 
} 
main( ) 
{ 
int a = 3, b = 4 
; 
} 
a > b 
if ( ) 
printf ( ”B” ) ; 
else 
printf ( ”A” ) ; 
Output: A Output: A
One More Way 
main( ) 
{ 
int a = 3, b = 4 ; 
if ( a <= b ) 
printf ( ”A” ) ; 
else 
printf ( ”B” ) ; 
} 
main( ) 
{ 
int a = 3, b = 4 ; 
if ( ) 
printf ( ”A” ) ; 
else 
printf ( ”B” ) ; 
} 
! a > b 
Output: A Output: B
The Correct Way 
main( ) 
{ 
int a = 3, b = 4 ; 
if ( ! ( a > b ) ) 
printf ( ”A” ) ; 
else 
printf ( ”B” ) ; 
} 
Output: A
Yet Another Way 
main( ) 
{ 
int a = 3, b = 4 ; 
if ( a > b ) 
printf ( ”B” ) ; 
else 
printf ( ”A” ) ; 
} 
Output: A 
main( ) 
{ 
int a = 3, b = 4 ; 
if ( ) 
printf ( ”B” ) ; 
else 
printf ( ”A” ) ; 
} 
! ( a <= b ) 
Output: A
What Would be The Output 
main( ) 
{ 
int a = 3, b = - 4, c ,d ; 
c = !a ; 
0 
d = !b ; 
printf ( ”%d %d %d %d”, c, d, a, b ) ; 
} 
0 
3 
- 4
Point Out The Error 
main( ) 
{ 
int a = 3, b = 4, c ; 
Lvalue 
Required 
c = !a || b = 7 ; 
printf ( ”%d %d”, b, c ) ; 
}
What Would be The Output 
main( ) 
{ 
int a = 3, b = 4, c ; 
c = !a || ( b = 7 ) ; 
printf ( ”%d %d”, b, c ) ; 
} 
7 1
Yet Another Way 
main( ) 
{ 
? : 
Conditional 
operators 
How are they 
different? 
main( ) 
{ 
int a = 3, b = 4 ; 
if ( a <= b ) 
printf ( ”A” ) ; 
else 
printf ( ”B” ) ; 
} 
Output: A 
a <= b printf ( ”A” ) printf ( ”B” ) ; 
} 
int a = 3, b = 4 ;
How Would You Convert 
main( ) 
{ 
int a = 3, b = 4 ; 
if ( a <= b ) 
printf ( ”A” ) ; 
} 
main( ) 
{ 
int a = 3, b = 4 ; 
a <= b ? printf ( ”A” ) ; 
} 
Error
Remove The Error 
main( ) 
{ 
int a = 3, b = 4 ; 
a <= b ? printf ( ”A” ) : ; 
} 
Necessary
No Errors At Last 
main( ) 
{ 
Null Statement 
int a = 3, b = 4 ; 
a <= b ? printf ( ”A” ) : ; ; 
} 
TTeerrmmiinnaattoorr 
Dummies 
Soln: a <= b ? printf ( ”A” ) : printf ( ” ” ) ; 
a <= b ? printf ( ”A” ) : ( z = 3 ) ;
Moral of The Story 
Use one, use another 
Dummy statement 
Not a replacement for if-else 
Only 1 statement in ? part 
and one in : part
Some More Different Ways 
main( ) 
{ 
int a = 3, b ; 
a <= 5 ? ( b = 10 ) : ( b = 20 ) ; 
printf ( ”%d”, b ) ; 
} 
b = a <= 5 ? 10 : 20 ; 
printf ( ”%d”, b ) ; 
( ) Necessary 
 printf ( ”%d”, a <= 5 ? 10 : 20 ) ;
Practice Hasn’t Harmed 
Anybody 
Exploring C Chapter 2 
Let Us C Chapter 2
Conversions 
main( ) 
{ 
10 20 3.140000 6.280000 
int a = 10, b = 20 ; 
float c = 3.14 , d = 6.28 ; 
printf ( ”%d %d %f %f ”, a, b, c, d ) ; 
printf ( ”%d %d %f %d ”, a, b, c, d ) ; 
printf ( ”%f %d %f %f ”, a, b, c, d ) ; 
} 10 20 3.140000 0 
2.68 e 154 7864 0.000000 -1.827 e 259
Conversions Continued... 
main( ) 
{ 
int i = 65, j = 90 ; 
char ch = ’A’, dh = ’Z’ ; 
65 90 A Z 
printf ( ”%d %d %c %c”, i, j, ch, dh ) ; 
A Z 
printf ( ”%c %c”, i, j ) ; 
printf ( ”%d %d”, ch, dh ) ; 
} 
65 90
int i = 65 ; float a = 3.14 char ch = ’A’ 
i a ch 
2 65 
2 32 1 
2 16 0 
2 8 0 
2 4 0 
2 2 0 
2 1 0 
0 1 
’A’ 
Divisions, 
multiplications 
illegal 
Some method involving 
division by 2 and 
multiplication by 
powers of 2 
Dec 65 Bin 01000001 
i a 
? 
3.14 
ch 
01000001 0’s and 1’s ?
Our Own Scheme 
 2 Binary Digits 00 01 10 11 
A B C D 
 3 Binary Digits 000 001 010 011 100 101 110 111 
A B C D E F G H 
 4 Binary Digits 24 = 16 combinations 
 5 Binary Digits 25 = 32 combinations 
00000 00001 00010 .. .. .. .. 26 Combinations 
A B C .. .. .. .. 26 Alphabets
Characters to be Represented 
 26 Capital letters ( A- Z ) 
 26 Smallcase letters ( a - z ) 
 10 Digits ( 0 - 9 ) 
 32 Special Symbols ( + - * / . : ; etc. ) 
 34 Non-printable chars - control chars 
 128 Graphic characters 
256 Characters
Our Own Scheme 
2 Bits 22 = 4 Combinations 
3 Bits 23 = 8 Combinations 
4 Bits 24 = 16 Combinations 
5 Bits 25 = 32 Combinations 
6 Bits 26 = 64 Combinations 
7 Bits 27 = 128 Combinations 
8  Bits 28 = 256 Combinations
Graphics Characters 
Character ASCII value 
218 
191 
192 
217 
196 
179
Workable Scheme? 
00001111 01001111 10101010 256 combinations 
A B C 256 characters 
01000001 A 
01000010 B 
01000011 C 
01000100 D 
01000101 E 
…. .. 
…. .. 
ASCII Codes
Binary Is Difficult 
Binary 
A 01000001 
B 01000010 
C 01000011 
D 01000100 
E 01000101 
.. …. 
Decimal 
65 
66 
67 
68 
69 
.. 
Character
Characters to be Represented 
 26 Capital letters ( 65 - 90 ) 
 26 Smallcase letters ( 97 - 122 ) 
 10 Digits ( 48 - 57 ) 
 32 Special Symbols ( ) 
 34 Non-printable chars ( 0 - 33 ) 
 128 Graphic characters ( 128 - 255 ) 
256 Characters ( 0 - 255 )
Methods Are Different 
int i = 65 ; char ch = ’A’ ; 
i ch 
01000001 01000001 
printf ( ”%c”, i ) ; 
printf ( ”%d”, ch ) ; 
A 
65
What About More Than 255 
int i = 300, j = 65; 
printf ( ”%c”, i ) ; 
printf ( ”%c”, j ) ; 
i 
Only lower byte 
gets used 
00000001 00101100 
Higher Lower 
j 
00000000 01000001
Why Unreliable Conversions 
float i = 3.14 ; 
printf ( ”%c”, i ) ; 
printf ( ”%d”, i ) ; 
i 
0s and 1s 0s and 1s 0s and 1s 0s and 1s 
int i = 35 ; 
printf ( ”%f”, i ) ; 
i 
0s and 1s 0s and 1s
Control Instructions 
 Sequence 
 Decision - if-else 
- ? : 
- < > <= >= = = != 
- && || ! 
 Repitition / Loop control instruction
To Begin With... 
main( ) 
{ 
int p, n ; 
float r, si ; 
printf ( ”Enter values of p, n, r” ) ; 
scanf ( ”%d%d%f”, &p, &n, &r ) ; 
si = p * n * r / 100 ; 
printf ( ”Simple interest = Rs %f”, si ) ; 
}
Repeat with while 
main( ) 
{ 
int p, n ; 
float r, si ; 
while 
printf ( ”Enter values of p, n, r” ) ; 
scanf ( ”%d%d%f”, &p, &n, &r ) ; 
si = p * n * r / 100 ; 
printf ( ”Simple interest = Rs %f”, si ) ; 
} 
Tip : while - Keyword
Any Condition 
main( ) 
{ 
int p, n, i ; 
float r, si ; 
while ( ) 
printf ( ”Enter values of p, n, r” ) ; 
scanf ( ”%d%d%f”, &p, &n, &r ) ; 
si = p * n * r / 100 ; 
printf ( ”Simple interest = Rs %f”, si ) ; 
} 
i <= 10
Initialization Necessary 
main( ) 
{ 
int p, n, i ; float r, si ; 
while ( i <= 10 ) 
printf ( ”Enter values of p, n, r” ) ; 
scanf ( ”%d%d%f”, &p, &n, &r ) ; 
si = p * n * r / 100 ; 
printf ( ”Simple interest = Rs %f”, si ) ; 
} 
i = 1 ;
So Also Incrementation 
main( ) 
{ 
int p, n, i ; float r, si ; 
i = 1 ; 
while ( i <= 10 ) 
printf ( ”Enter values of p, n, r” ) ; 
scanf ( ”%d%d%f”, &p, &n, &r ) ; 
si = p * n * r / 100 ; 
printf ( ”Simple interest = Rs %f”, si ) ; 
} 
i = i + 1 ;
Scope And Default Scope 
main( ) 
{ 
int p, n, i = 1 ; float r, si ; 
while ( i <= 10 ) 
{ 
printf ( ”Enter values of p, n, r” ) ; 
scanf ( ”%d%d%f”, &p, &n, &r ) ; 
si = p * n * r / 100 ; 
printf ( ”Simple interest = Rs %f”, si ) ; 
i = i + 1 ; 
} 
}
Logic Doesn’t Matter 
main( ) 
{ 
int i ; 
i = 1 ; 
while ( i <= 10 ) 
{ 
/* any logic */ 
i = i + 1 ; 
} 
} 
Loop Counter / 
Index variable 
/* Initialisation of loop counter */ 
/* Testing of loop counter */ 
/* Incrementation of loop counter*/
More On Loop Counters 
 Loop counters can be integers, long 
integers, floats or characters 
 Loop counters can be incremented 
or decremented by any suitable step 
value
Another Program 
/* Print numbers from 1 to 10 */ 
main( ) 
{ 
int i = 1 ; 
printf ( ”%d”, i ) ; 
}
Put it in a Loop 
/* Print numbers from 1 to 10 */ 
main( ) 
{ 
int i = 1 ; 
while ( i <= 10 ) 
{ 
printf ( ”%d”, i ) ; 
} 
} 
i = i + 1 ;
Another Way 
/* Print numbers from 1 to 10 */ 
main( ) 
{ 
int i = 1 ; 
while ( i <= 10 ) 
{ 
printf ( ”%d”, i ) ; 
} 
} 
i ++ ;
Incrementation / 
Decrementation Operators 
++ Increases value of a variable by 1 
 -- Decreases value of a variable by 1 
 ** 
// 
%% 
Don’t make 
sense
Are They OK 
 i = i + 1 i++ 
 i = i + 2 i+++ 
 i = i + 10 ? 
Tip : +++ doesn’t exist
Would This Work 
 
 
 
 i = j---2 ; 
 int i, j = 3 ; 
i = --j ; 
i = --3 ; 
 i = j----2 ; 
 i = j----2 ; 
 

Space Matters 
 
 
i = j----2 ; 
i = j-- - -2 ; 
Optional Complusory
Is i++ Same As ++i 
main( ) 
{ 
int i = 1 ; 
while ( i <= 10 ) 
1 st oper. - Printing 
2 nd oper. - Incrementation 
printf ( ”%d”, i++ ) ; 
} 
Output : 1 2 3 4 ……9 10
main( ) 
{ 
int i = 1 ; 
while ( i <= 10 ) 
1 st oper. - Incrementation 
2 nd oper. - Printing 
printf ( ”%d”, ++i ) ; 
} 
Output : 
Using ++i 
2 3 4 5 ……9 10 11
The Correct Way 
main( ) 
{ 
int i = 0 ; 
while ( i < 10 ) 
printf ( ”%d”, ++i ) ; 
} 
Output : 1 2 3 4 ……9 10
Moral? 
main( ) 
{ 
int i = 1 ; 
while ( i <= 10 ) 
printf ( ”%d”, i++ ) ; 
} 
main( ) 
{ 
int i = 0 ; 
while ( i < 10 ) 
printf ( ”%d”,++ i ) ; 
}
Two More Ways 
main( ) 
{ 
int i = 0 ; 
while ( i++ < 10 ) 
printf ( ”%d”, i ) ; 
} 
main( ) 
{ 
int i = 0 ; 
while ( ++i <= 10 ) 
printf ( ”%d”, i ) ; 
} 
1 st oper. - Incr 
2 nd oper. - Testing 
1 st oper. - Testing 
2 nd oper. - Incr
Compare 
main( ) 
{ 
int i = 1 ; 
while ( i <= 10 ) 
printf ( ”%d”, i++ ) ; 
} 
main( ) 
{ 
int i = 0 ; 
while ( i < 10 ) 
printf ( ”%d”,++ i ) ; 
} 
main( ) 
{ 
int i = 0 ; 
while ( ++i <= 10 ) 
printf ( ”%d”, i ) ; 
} 
main( ) 
{ 
int i = 0 ; 
while ( i++ < 10 ) 
printf ( ”%d”, i ) ; 
}
While ( We Don’t Meet Again ) 
Let Us C Page 96-99 ( f )
Print Nos. From 1 to 10 
main( ) 
{ 
int i = 1 ; 
while ( i <= 10 ) 
{ 
printf ( ”%d”, i ) ; 
} 
} 
a = a * 10 ; 
a *= 10 ; 
b = b % 5 ; 
b %= 5 ; 
i += 1 ; 
Same as i++, 
++i, i = i + 1 
+= -= *= /= %= Compound Assignment Oper.
One More Way 
main( ) 
{ 
int i = 1 ; 
while ( ) 
{ 
Any Non-Zero 
number 
printf ( ”%d”, i ) ; 
i++ ; 
} 
} 
1 /* Repeat infinite times */
Applying Brakes 
main( ) 
{ 
break - Terminates Loop 
exit( ) - Terminates Program 
int i = 1 ; 
while ( 1 ) /* Repeat infinite times */ 
{ 
printf ( ”%d”, i ) ; 
i++ ; 
} 
} 
if ( i > 10 ) 
break ; 
if ( i > 10 ) 
exit( ) ; 
Keyword
Calculate 
n! 
n 
i 
i = 1 
xn 
1 * 2 * 3 * 4 *….n 
1 + 2 + 3 + 4 +.…n 
2 * 2 * 2 * 2 *….n 
Running product 
Running sum 
Running product
Running Sum And Products 
int s, p, pr, i, n ; 
i = 1 ; 
while ( i <= n ) 
{ 
s = 0 ; p = 1 ; pr = 1 ; 
s = s + ; 
} 
i 
p = p * i ; 
pr = pr * 2 ; 
1 + 2 + 3 + 4 +.…n RS 
1 * 2 * 3 * 4 *….n RP 
2 * 2 * 2 * 2 *….n RP 
main( ) 
{ 
scanf ( ”%d”, &n ) ; 
i++ ; 
printf ( ”%d %d %d”, s, p, pr ) ; 
} 
i s 
1 0 
2 1 
3 3 
4 6 
5 10 
6 15 
5
x x3 x5 x7 x9 — - — + — - — + — - …………10 terms 
1! 3! 5! 7! 9! 
main( ) 
{ 
scanf ( ”%f”, &x ) ; 
while ( i <= 10 ) 
{ 
i = 1 ; s = 0 ; 
calculate numerator 
calculate denominator 
term = numerator / denominator 
Add / subtract alternate terms to/from s 
i++ ; 
} 
print ( s ) ; 
}
main( ) 
{ 
scanf ( ”%f ”, &x ) ; 
i = 1 ; 
while ( i <= 10 ) 
{ 
OK ? 
float x, s, n, d, t ; int i, j, k, n ; 
k = n = d = 1 ; 
i++ ; 
} 
printf ( ”%f ”, s ) ; 
} 
x1 x3 x5 —1! - — 3! + — 5! - 
i j 
1 1 
2 3 
3 5 
4 7 
5 9 
.. .. 
j = 2 * i - 1 ; 
while ( k <= j ) 
{ 
d = d * k ; n = n * x ; 
k++ ; 
} 
t = n / d ; 
( i % 2 == 0 ) ? s = s - t : s = s + t ; 
Anything 
wrong ? 
s = 0 ; 
Associates 
from R to L
Prime Number or Not 
main( ) 
{ 
printf ( ”Enter any number” ) ; 
scanf ( ”%d”, &n ) ; 
... 
... 
} 
13 
2 3 4 .. .. .. 12 
int n ; 
13
Prime No. 
main( ) 
{ 
int n ; 
printf ( ”Enter any number” ) ; 
scanf ( ”%d”, &n ) ; 
} 
13 
2 3 4 .. .. .. 12 
int i = 2 ; 
while ( ) 
i <= n - 1 
{ 
if ( n % i = = 0 ) 
printf ( ”Not a prime number” ) ; 
else 
i++ ; 
}
main( ) 
{ 
int n, i = 2 ; 
printf ( ”Enter any number” ) ; 
scanf ( ”%d”, &n ) ; 
while ( i <= n - 1 ) 
13 
{ 
if ( n % i == 0 ) 
printf ( ”Not a prime number” ) ; 
break ; 
{ 
} 
else 
i++ ; 
} 
} 
if ( i == n ) 
printf ( ”Prime number” ) ;
Generate Prime Numbers 
2 17 
3 19 
5 23 
7 . 
11 . 
13 199
i = ++a && ++b && ++c ; 
j = ++a || ++b || ++c ; 
What Would Be The O/P 
b = ( ++i && ++j ) || ++k ; 
main( ) 
{ 
int i = 5, j = 4, k = -1, a, b ; 
a = i && !j ; 
b = ++i && ++j || ++k ; 
printf ( ”%d %d %d %d %d”, a, b, i, j, k ) ; 
} 
b = ++i && ( ++j || ++k ); 
0 1 6 5 -1
Loop Control Instructions 
while 
for 
 
do-while
The while Loop 
main( ) 
{ 
int m1, m2, m3, avg, i ; 
i = 1 ; 
while ( i <= 10 ) 
{ 
scanf ( ”%d%d%d”, &m1, &m2, &m3 ) ; 
avg = ( m1 +m2 + m3 ) / 3 ; 
printf ( ”%d”, avg ) ; 
i++ ; 
} 
} 
Init Test Incr 
Test 
Test 
Incr 
Incr 
Test Incr 
.. .. 
.. ..
The for Loop 
main( ) 
{ 
int m1, m2, m3, avg, i ; 
scanf ( ”%d%d%d”, &m1, &m2, &m3 ) ; 
avg = ( m1 + m2 + m3 ) / 3 ; 
printf ( ”%d”, avg ) ; 
} 
( ) 
Init Test Incr 
Test 
Test 
Incr 
Incr 
Test Incr 
.. .. 
for .. .. i = 1 ; i <= 10 ; i++ 
{ 
}
Comparision - I 
Infinite Loop Finite Loop 
main( ) 
{ 
int i = 1 ; 
while ( i <= 10 ) 
statement1 ; 
statement2 ; 
statement3 ; 
i++ ; 
} 
main( ) 
{ 
int i ; 
for ( i = 1 ; i <= 10 ; i++ ) 
statement1 ; 
statement2 ; 
statement3 ; 
}
Comparison II 
Infinite Loop Finite Loop 
main( ) 
{ 
int i = 1 ; 
while ( i <= 10 ) ; 
{ 
statement1 ; 
statement2 ; 
statement3 ; 
i++ ; 
} 
} 
main( ) 
{ 
int i ; 
for ( i = 1 ; i <= 10 ; i++ ) ; 
{ 
statement1 ; 
statement2 ; 
statement3 ; 
} 
}
Print Nos. From 1 to 10 
main( ) 
{ 
int i ; 
for ( i = 1 ; i <= 10 ; i++ ) 
printf ( ”n%d”, i ) ; 
}
Dropping Initialisation 
main( ) 
{ 
int i = 1 ; 
for ( ; i <= 10 ; i++ ) 
printf ( ”n%d”, i ) ; 
}
Dropping Incrementation 
main( ) 
{ 
int i = 1 ; 
for ( ; i <= 10 ; ) 
{ 
printf ( ”n%d”, i ) ; 
i++ ; 
} 
}
Dropping Condition 
main( ) 
; ; Necessary 
{ 
int i = 1 ; 
for ( ; ; ) 
{ 
printf ( ”%d”, i ) ; 
i++ ; 
} 
} 
if ( i > 10 ) 
break ; 
printf ( ”%d”, i++ ) ; 
if ( ++i > 10 )
Comparison III 
Error Infinite Loop 
main( ) 
{ 
int i = 1 ; 
while ( ) 
{ 
statement1 ; 
statement2 ; 
statement3 ; 
} 
} 
main( ) 
{ 
int i = 1 ; 
for ( ; ; ) 
{ 
statement1 ; 
statement2 ; 
statement3 ; 
} 
}
Drawing Boxes 
012..... 
24 
0 1 2 … … … … 79 
(10,20) 
Tip : ( 10, 20 ) 
(23,70) 
( row number, col number )
Box Drawing Characters 
Character ASCII value 
218 
191 
192 
217 
196 
179
To Begin With... 
main( ) 
{ 
clrscr( ) ; 
printf ( ”%c”, 218 ) ; 
… 
… 
}
main( ) 
{ 
clrscr( ) ; 
} 
{} int r, c ; 
(10,20) 
gotorc ( 10, 20 ) ; printf ( ”%c”, 218 ) ; 
gotorc ( 10, 70 ) ; printf ( ”%c”, 191 ) ; 
gotorc ( 23, 20 ) ; printf ( ”%c”, 192 ) ; 
gotorc ( 23, 70 ) ; printf ( ”%c”, 217 ) ; 
for ( r = 11 ; r < 23 ; r++ ) 
{ 
gotorc ( r, 20 ) ; printf ( ”%c”, 179 ) ; 
gotorc ( r, 70 ) ; printf ( ”%c”, 179 ) ; 
} 
for ( … … … .. ) 
(23,70)
#include ”goto.c” 
main( ) 
{ 
int r, c ; 
clrscr( ) ; 
gotorc ( 10, 20 ) ; printf ( ”%c”, 218 ) ; 
.. .. 
.. .. 
for ( r = 11 ; r < 23 ; r++ ) 
{ 
.. .. 
} 
for ( … … … ) 
{} 
}
96 - 99 
Chapter 3
1 1 1 
1 1 2 
1 1 3 
1 2 1 
1 2 2 
1 2 3 
1 3 1 
1 3 2 
1 3 3 
3 1 1 
3 1 2 
3 1 3 
3 2 1 
3 2 2 
3 2 3 
3 3 1 
3 3 2 
3 3 3 
Combinations 
2 1 1 
2 1 2 
2 1 3 
2 2 1 
2 2 2 
2 2 3 
2 3 1 
2 3 2 
2 3 3
Starting Off... 
i = 1 ; 
j = 1 ; 
for ( k = 1 ; k <= 3 ; k++ ) 
{ 
printf ( ”n%d%d%d”, i, j, k ) ; 
} 
} 
1 1 1 
1 1 2 
1 1 3 
1 2 1 
1 2 2 
1 2 3 
1 3 1 
1 3 2 
1 3 3 
main( ) 
{ 
int i, j, k ; 
i j k
Adding One More Loop 
main( ) 
{ 
int i, j, k ; 
i = 1 ; 
for ( k = 1 ; k <= 3 ; k++ ) 
printf ( ”n%d%d%d”, 
i, j, k ) ; 
} 
for ( j = 1 ; j <= 3 ; j++ ) 
{ 
i j k 
1 1 1 
1 1 2 
1 1 3 
1 2 1 
1 2 2 
1 2 3 
1 3 1 
1 3 2 
1 3 3 
}
main( ) 
{ 
int i, j, k; 
for ( j = 1 ; j <= 3 ; j ++ ) 
{ 
for ( k = 1 ; k <= 3 ; k++ ) 
printf ( ”n%d%d%d”, i, j, k ) ; 
} 
} 
for ( i = 1 ; i <= 3 ; i ++ ) 
{ 
1 1 1 
.. .. .. 
.. .. .. 
1 3 3 
2 1 1 
.. .. .. 
.. .. .. 
2 3 3 
3 1 1 
.. .. .. 
.. .. .. 
3 3 3 
} 
Finishing Off...
Unique Combinations 
main( ) 
{ 
int i, j, k ; 
for ( i = 1 ; i <= 3 ; i++ ) 
{ 
for ( j = 1 ; j <= 3 ; j++ ) 
{ 
for ( k = 1 ; k <= 3 ; k++ ) 
{ 
printf ( ”%d %d %d”, i, j, k ) ; 
} 
} 
} 
} 
.. .. .. 
1 2 3 
.. .. .. 
1 3 2 
.. .. .. 
2 1 3 
.. .. .. 
2 3 1 
.. .. .. 
3 1 2 
.. .. .. 
3 2 1 
if ( i != j && j != k && k != i ) 
 
if ( i != j != k ) 
printf ( ”%d %d %d”, i, j, k ) ;
One More Way 
main( ) 
{ 
int i, j, k ; 
for ( i = 1 ; i <= 3 ; i++ ) 
{ 
for ( j = 1 ; j <= 3 ; j++ ) 
{ 
for ( k = 1 ; k <= 3 ; k++ ) 
{ 
printf ( ”%d %d %d”, i, j, k ) ; 
} 
} 
} 
} 
i j k 
1 1 1 
2 1 
3 1 
if ( i = = j || j = = k || k = = i ) 
break ; 
else
The Correct Way 
main( ) 
{ 
int i, j, k ; 
for ( i = 1 ; i <= 3 ; i++ ) 
{ 
for ( j = 1 ; j <= 3 ; j++ ) 
{ 
for ( k = 1 ; k <= 3 ; k++ ) 
{ 
if ( i == j || j == k || k == i ) 
continue ; 
else 
printf ( ”%d %d %d”, i, j, k ) ; 
} 
} 
} 
} 
i j k 
1 1 1 
2 
3 
1 
2 
3 
2 
4 
Would ; 
be OK?
Multiple Initializations 
main( ) 
{ 
int i, j, k ; 
i = 1 ; j = 3 ; 
for ( k = 1 ; k <= 5 ; k++ ) 
{ 
…… 
i += 2 ; 
j *= 4 ; 
} 
}
Better Way 
for ( i = 1, j = 3, k = 1 ; k <= 5 ; i += 2, j *= 4 ) 
{ 
…… 
} 
Multiple 
Conditions? 
Can I Drop 
k++ 
Connect using 
&& and ||
Type of Loops 
 while 
 for 
 
Which is 
 do - while better
The do-while Loop 
main( ) 
{ 
int i = 1 ; 
do 
{ 
printf ( ”n%d”, i ) ; 
i++ ; 
} while ( i <= 10 ) ; 
} 
Tip1: { } - Necessary 
Tip2: ; after while - Necessary
Compare 
do 
{ 
printf ( ”Hi” ) ; 
} while ( 4 < 1 ) ; 
for ( ; 4 < 1 ; ) 
printf ( ”Hi” ) ; 
while ( 4 < 1 ) 
printf ( ”Hi” ) ; 
Hi No 
Output 
No 
Output
Which To Use When 
 for - Finite no. of times 
 while - Unknown no. of times 
 do - while - At least once 
Recommended, 
Not compulsory
Unknown No. of Times 
Foolproof? 
while ( ) 
{ 
} 
} 
char ch = ’y’ ; 
ch == ’y’ 
main( ) 
{ 
/* some logic */ 
printf ( ”Continue Y/N” ) ; 
scanf ( ”%c”, &ch ) ; 
Solution 
while ( ch == ’y’ || ch == ’Y’ ) 
while ( toupper ( ch ) == ’Y’ ) 
while ( tolower ( ch ) == ’y’ )
Control Instructiopns 
 Sequence 
 Decision 
 Repetition 
 
- while, for, do-while 
break, continue 
- ++, -- 
- +=, -=, *=, /=, %= 
 Case
main ( ) 
{ 
int n ; 
printf ( ”Enter no. between 1 and 3” ) ; 
scanf ( ”%d”, &n ) ; 
if ( n = = 1 ) 
printf ( ”You entered 1” ) ; 
else 
{ 
if ( n = = 2 ) 
printf ( ”You entered 2” ) ; 
else 
{ 
if ( n = = 3 ) 
printf ( ”You entered 3” ) ; 
else 
printf ( ”Wrong choice” ) ; 
} 
} 
} 
Three 
Problems
Alternative 
Use Logical Operators 
 
 
Use case Control Instruction
main( ) 
{ 
int n ; 
printf ( ”Enter no. below 1 and 3” ) ; 
scanf ( ”%d”, &n ) ; 
{ 
case 1 : 
printf ( ”You Entered 1” ) ; 
case 2 : 
printf ( ”You Entered 2” ) ; 
case 3 : 
printf ( ”You Entered 3” ) ; 
else 
printf ( ”Wrong Choice” ) ; 
} 
} 
switch ( n ) 

main( ) 
{ 
Tip: If a case fails control jumps 
to the next case 
int n ; 
printf ( ”Enter no. between 1 and 3” ) ; 
scanf ( ”%d”, &n ) ; 
{ 
case 1 : 
2 Output: 
You entered 2 
printf ( ”You entered 1” ) ; 
case 2 : 
printf ( ”You entered 2” ) ; 
case 3 : 
printf ( ”You entered 3” ) ; 
} 
} 
switch ( n ) 
default : 
printf ( ”Wrong Choice” ) ;
main ( ) 
{ 
int n ; 
scanf ( ”%d”, &n ) ; 
switch ( n ) 
{ 
case 1 : 
printf ( ”You entered 1” ) ; 
case 2 : 
printf ( ”You entered 2” ) ; 
case 3 : 
printf ( ”You entered 3” ) ; 
default : 
printf ( ”Wrong choice” ) ; 
} 
} 
Tip: If a case is satisfied all statements 
below it are executed 
Output: 
You entered 2 
You entered 3 
2 Wrong choice
main ( ) 
{ 
int n ; 
scanf ( ”%d”, &n ) ; 
switch ( n ) 
{ 
case 1 : 
printf ( ”You Entered 1” ) ; 
case 2 : 
printf ( ”You Entered 2” ) ; 
case 3 : 
printf ( ”You Entered 3” ) ; 
default : 
printf ( ”Wrong choice” ) ; 
} 
} 
The Solution 
break ; 
break ; 
break ; 
2
main ( ) 
{ 
int n ; 
scanf ( ”%d”, &n ) ; 
switch ( n ) 
{ 
case 1 : 
printf ( ”You Entered 1” ) ; break ; 
case 2 : 
printf ( ”You Entered 2” ) ; continue ; 
case 3 : 
printf ( ”You Entered 3” ) ; break ; 
default : 
printf ( ”Wrong choice” ) ; 
} 
} 
Illegal 
What If continue
main ( ) 
{ 
int n ; 
scanf ( ”%d”, &n ) ; 
switch ( n ) 
{ 
case 2 : 
printf ( ”You Entered 2” ) ; break ; 
case 1 : 
printf ( ”You Entered 1” ) ; break ; 
case 3 : 
printf ( ”You Entered 3” ) ; break; 
default : 
printf ( ”Wrong choice” ) ; 
} 
} 
Tip: Order of cases is unimportant
main ( ) 
{ 
int n ; 
scanf ( ”%d”, &n ) ; 
switch ( n ) 
{ 
default : 
printf ( ”Wrong choice” ) ; 
case 2 : 
break ; 
printf ( ”You Entered 2” ) ; break ; 
case 1 : 
printf ( ”You Entered 1” ) ; break ; 
case 3 : 
printf ( ”You Entered 3” ) ; break ; 
} 
} 
Tip: Even default can be the very first case
main ( ) 
{ 
int n ; 
scanf ( ”%d”, &n ) ; 
switch ( n ) 
{ 
case 2 : 
printf ( ”You Entered 2” ) ; break ; 
case 1 : 
printf ( ”You Entered 1” ) ; break ; 
case 3 : 
printf ( ”You Entered 1” ) ; 
} 
} 
Tip: default case is optional
main( ) 
{ 
char ch ; 
printf ( ”Enter alphabet between A and C” ) ; 
scanf ( ”%c”, &ch ) ; 
switch ( ch ) 
{ 
case ’A’: 
printf ( ”You entered A” ) ; 
break ; 
case ’B’: 
printf ( ”You entered B” ) ; 
break ; 
case ’C’: 
printf ( ”You entered C” ) ; 
break ; 
} 
}
main( ) 
{ 
char ch ; 
printf ( ”Enter alphabet between A and C” ) ; 
scanf ( ”%c”, &ch ) ; 
switch ( ch ) 
{ 
case ’A’ || ’a’ : 
printf ( ”You entered A” ) ; 
break ; 
case ’B’ || ’b’ : 
printf ( ”You entered B” ) ; 
break ; 
case ’C’ || ’c’ : 
printf ( ”You entered C” ) ; 
break ; 
} 
} 
Become 
case 1 :
main( ) 
{ 
char ch ; 
printf ( ”Enter alphabet between A and C” ) ; 
scanf ( ”%c”, &ch ) ; 
switch ( ch ) 
{ 
case ’A’: 
printf ( ”You entered A” ) ; 
break ; 
case ’B’: 
printf ( ”You entered B” ) ; 
break ; 
case ’C’: 
printf ( ”You entered C” ) ; break ; 
} 
} 
case ’a’ : A 
case ’b’ : 
case ’c’ : 
a
main( ) 
{ 
int n = 2 ; 
int a = 1, b = 2 ; 
switch ( n ) 
{ 
case a : 
… 
case b : 
… 
case 3 : 
… 
} 
} 
Would This Work 

General Form 
main( ) 
{ 
n + 3 / a + 2 4 + 3 / 5 + 2 
switch ( expression ) 
{ 
case constant expression : 
… 
case constant expression : 
… 
case constant expression : 
… 
default: 
… 
} 
} 
3 + 2 % 5
Checking switch 
 int 
 char 
 long int 
 float 
 
 
 
? 
Tip: float cannot be checked using switch
Is switch a 
replacement 
for if ?
99 [D] - 102 
Chapter 3
Menu Management 
1. Odd / Even 
2. Leap Year 
3. Prime No. 
0. Exit 
Your Choice? 
20 
10
# include ”goto.c” 
main( ) 
{ 
clrscr( ) ; 
gotorc ( 10, 20 ) ; printf ( ”1. Odd / Even” ) ; 
gotorc ( 11, 20 ) ; printf ( ”2. Leap year” ) ; 
gotorc ( 12, 20 ) ; printf ( ”3. Prime number” ) ; 
gotorc ( 13, 20 ) ; printf ( ”0. Exit” ) ; 
gotorc ( 15, 20 ) ; printf ( ”Your choice?” ) ; 
scanf ( ”%d”, &choice ) ; 
.. 
.. 
} 
int choice ;
main( ) 
{ 
int choice ; 
/* display menu */ 
scanf ( ”%d”, &choice ) ; 
switch ( choice ) 
{ 
case 1 : 
/* odd / even logic */ 
break ; 
case 2 : 
/* leap year logic*/ 
break ; 
case 3 : 
/*prime number logic*/ 
break ; 
default : 
printf ( ”a” ) ; 
} 
} 
case 0 : 
break ; 
n t a 
Escape 
Sequences 
printf ( ”aaa” ) ; 
Long 
Beep
switch ( choice ) 
{ 
case 1 : 
break ; 
case 2 : 
break ; 
case 3 : 
break ; 
case 0 : 
break ; 
default : 
printf ( ”a” ) ; 
} 
Block Commands 
CH2PR3.C 
Odd / even 
logic 
CH2PR4.C 
Leap year 
logic 
CH2PR5.C 
Prime no. 
logic
Other Block Commands 
Ctrl KB 
Ctrl KK 
Mark block 
Ctrl KV Move block 
Ctrl KC Copy block 
Ctrl KW Write block to file 
Ctrl KY Delete block 
Ctrl KH Hide or unmark block
Readjustments Necessary 
 case 1: 
main( ) 
{ 
Delete Delete 
…… 
} 
break ; 
 Shift declarations 
Only Shifting 
Won’t Do
No while, No Menu 
#include ”goto.c” 
main( ) 
{ 
while ( 1 ) 
{ 
/*display menu*/ 
scanf ( ”%d”, &choice ) ; 
switch( choice ) 
{ 
case 1 : 
… 
break ; 
case 0 : 
break ; 
} 
} 
int choice ; 
} exit( ) ;
Requirements of A Menu 
 Infinite loop 
 switch
Surprised? 
main( ) 
{ 
float a = 0.7 ; 
if ( a < 0.7 ) 
printf ( ”A” ) ; 
else 
printf ( ”B” ) ; 
} 
A
Appearances Are 
Misleading main( ) 
{ 
float a = 0.7 ; 
printf ( ”%d%d”, sizeof ( a ), sizeof ( 0.7 ) ); 
} 
365 0.7 
Integer Double 
4 8
Binary of A Float 
float a = 5.375 ; 
Binary of 5 Binary of .375 
101 .011 
Binary of 5.375 
101.011 
Normalised form 
1.01011 * 22 
.375 * 2 = 0.750 0 
.750 * 2 = 1.500 1 
.500 * 2 = 1.000 1
The Actual Storage 
Mantissa 
32 
0 1000 0001 01011000000000000000000 
Positive 
2 + 127 
. 01011 
1 8 23 
Sign 
Bit Biased 
Exponent
Difference In double 
Mantissa 
64 
1 11 52 
Bias value 
is 1023 
Sign 
Bit 
Biased 
Exponent
Binary of 0.7 
.7 * 2 = 1.4 1 
.4 * 2 = 0.8 0 
.8 * 2 = 1.6 1 
.6 * 2 = 1.2 1 
.2 * 2 = 0.4 0 
.4 * 2 = 0.8 0 
.8 * 2 = 1.6 1 
.6 * 2 = 1.2 1 
.2 * 2 = 0.4 0 
.4 * 2 = 0.8 0 
. . . . . . 
. . . . . .
32 - bit 
Recurring binary 
equivalent of 
0.7 
64 - bit 
Recurring binary 
equivalent of 
0.7 
< 
float a = 0.7 ; 
if ( a < 0.7 ) 
printf ( ”A” ) ; 
else 
printf ( ”B” ) ; 
A
Overriding Defaults 
365 365L 
0.7 0.7f 
if ( a < 0.7f ) 
printf ( ”A” ) ; 
else 
printf ( ”B” ) ; 
B
Exact Binary 
main( ) 
{ 
float a = 5.375 ; 
if ( a < 5.375 ) 
printf ( ”A” ) ; 
else 
printf ( ”B” ) ; 
} 
B
Functions 
main( ) 
{ 
printf ( ”n I am in main” ) ; 
} 
bombay( ) 
{ 
printf ( ”n I am in Bombay” ) ; 
} 
kanpur( ) 
{ 
printf ( ”n I am in Kanpur” ) ; 
} 
Output: 
I am in main 
Functions 
main( ) 
printf( ) 
scanf( ) 
getch( ) 
exit( ) 
gotorc( ) 
clrscr( ) 
for( ) 
while( ) 
if( ) 
switch( )
Categories 
Functions 
Std. Library User-Defined 
printf( ) 
scanf( ) 
exit( ) 
clrscr( ) 
kanpur( ) 
bombay( ) 
gotorc( ) 
main( )
Calling Functions 
main( ) 
{ 
printf ( ”n I am in main” ) ; 
bombay( ) 
{ 
printf ( ”n I am in Bombay” ) ; 
} 
kanpur( ) 
{ 
printf ( ”n I am in Kanpur” ) ; 
} 
Output : 
I am in main 
I am in Bombay 
bombay( ) ; kanpur( ) ; I am in Kanpur 
} 
Function Call 
Function Def.
Tips 
 A C program is nothing but a collection 
of 1 or more functions 
 If C program contains 1 function its name 
must be main( ) 
 If C program contains more than 1 function 
then one of them has to be main( ) 
 Execution of any C program always begins 
with main( ) 
 Function names in a program must be unique
Order, Order ! 
bombay( ) 
{ 
printf ( ”n I am in Bombay” ) ; 
} 
main( ) 
{ 
printf ( ”I am in main” ) ; 
bombay( ) ; 
} 
Tip: Functions can be defined in any order
More Calls, More Bills 
main( ) 
{ 
printf ( ”n I am in main” ) ; 
bombay( ) ; 
bombay( ) ; 
} 
bombay( ) 
{ 
printf ( ”n I am in Bombay” ) ; 
} 
Tip: More the calls, slower the execution
Nobody is Nobody’s Boss 
main( ) 
{ 
kanpur( ) 
{ 
printf ( ”n I am in Kanpur” ) ; 
bombay( ) ; 
} 
printf ( ”n I am in main” ) ; 
bombay( ) ; kanpur( ) ; 
} 
bombay( ) 
{ 
printf ( ”n I am in Bombay” ) ; 
kanpur( ) ; 
} Tip: Any function can call 
any other function
Local v/s STD v/s ISD Calls 
main( ) 
{ 
printf ( ”n I am in main” ) ; 
main( ) ; 
} 
Local Call - Recursive Function 
Process - Recursion
115 - 120 
Chapter 4
Break From The Outermost Loop 
main( ) 
{ 
for ( i = 1 ; i <= 22 ; i+= 2 ) 
{ 
for ( j = 5 ; j <= 50 ; j++ ) 
{ 
for ( k = 1 ; k <= 9 ; k += 3 ) 
{ 
… … 
if ( i + j % k >= 2 ) 
break ; 
} 
} 
} 
} 
break ; 
break ; 
Go to work 
only once 
if ( i + j % k >= 2 ) 
break ;
main( ) 
{ 
for ( i = 1 ; i <= 22 ; i+= 2 ) 
{ 
for ( j = 5 ; j <= 50 ; j++ ) 
{ 
for ( k = 1 ; k <= 9 ; k += 3 ) 
{ 
… … 
if ( i + j % k >= 2 ) 
goto out ; 
} 
} 
} 
} 
Better Way... 
out : 
; 
Never use 
a goto
main( ) 
{ 
in : 
goto there ; 
for ( i = 1 ; i <= 22 ; i+= 2 ) 
{ 
for ( j = 5 ; j <= 50 ; j++ ) 
{ 
there : 
for ( k = 1 ; k <= 9 ; k += 3 ) 
{ 
… … 
if ( i + j % k >= 2 ) 
} goto out ; 
} 
} 
} 
Where Am I... 
out : 
goto in ;
Control Instructions 
 Sequence 
 Decision 
 Repitition 
 Case 
 goto
Communication 
main( ) 
{ 
int a = 10, b = 20, c = 30 ; 
calsum ( ) ; 
printf ( ”%d”, s ) ; 
} 
calsum( ) 
{ 
int a, b, c, s ; 
} 
int s ; 
s = a + b + c ; 
printf ( ”%d”, s ) ; 
Garbage 
Garbage
Passing Values 
main( ) 
{ 
Actual 
Arguments 
int a = 10, b = 20, c = 30 ; int s ; 
calsum ( a, b, c 
) ; 
printf ( ”%d”, s ) ; 
} 
calsum( ) 
{ 
int s ; 
} 
int x, int y, int z 
s = x + y + z ; 
printf ( ”%d”, s ) ; 
Garbage 
Formal 
Arguments 
60
main( ) 
{ 
Returning Values 
int a = 10, b = 20, c = 30, s ; 
calsum ( a, b, c ) ; 
} 
calsum ( int x, int y, int z ) 
{ 
int ss ; 
ss = x + y + z ; 
return ( ss ) ; 
} 
s = calsum ( a, b, c ) ; 
printf ( ”%d”, s ) ; 
60 
return ; Returns only 
control 
Return control 
and value 
return ( ss ) ; 
return ( 60 ) ; 
return ( x + y + z ) ; 

Are These Calls OK? 
calsum ( a, 25, d ) ; 
calsum ( 10 + 2, 25 % 3, d ) ; 
calsum ( a, calsum ( 25, 10, 4 ), d ) ; 
d = calsum ( a, 25, d ) * calsum ( a, 25, d ) + 23 ; 
calsum ( int x, int y, int z ) 
{ 
int ss ; 
ss = x + y + z ; 
return ( ss ) ; 
} 
 
Nested calls are legal. 
Call within an expression are legal.
Returning More Than 1 Value 
main( ) 
{ 
int a = 10, b = 20, c = 30 ; 
ssu, mp p= rsoudm( par,o bd, c( )a ;, b , c ) ; 
printf ( ”%d%d”, s, p ) ; 
} 
sumprod ( i n t x , i n t y , i n t z ) 
ss = x + y + z ; 
pp = x * y * z ; 
{ 
int ss, pp ; 
return ( ss, pp ) ; 
} 
int s, p ; 
 
 
A function can return only 
1 value at a time
main( ) 
{ 
One More Try 
int a = 10, b = 20, c = 30 ; 
int s, p ; 
s = sumprod ( a, b, c ) ; 
p = sumprod ( a, b, c ) ; 
printf ( ”%d%d”, s, p ) ; 
} 
sumprod ( int x, int y, int z ) 
{ 
ss = x + y + z ; 
pp = x * y * z ; 
return ( ss ) ; 
return ( pp ) ; 
} 
int ss, pp ; 
60 60 
Redundant
main( ) 
{ 
The Only Way Out 
int a = 10, b = 20, c = 30 ; int s, p ; 
s = sumprod ( a, b, c ) ; 
p = sumprod ( a, b, c ) ; 
printf ( ”%d%d”, s, p ) ; 
} 
, 1 
, 2 
sumprod ( int x, int y, int z, ) 
{ 
int ss, pp ; 
ss = x + y + z ; pp = x * y * z ; 
} 
int code 
if ( code == 1 ) 
return ( ss ) ; 
else 
return ( pp ) ; 
Sum, Product, 
Average, Variance 
Standard Deviation
A Better Way 
sumprod ( int x, int y, int z, int code ) 
{ 
int ss, pp ; 
ss = x + y + z ; 
pp = x * y * z ; 
} 
if ( code == 1 ) 
return ( ss ) ; 
else 
return ( pp ) ; 
code == 1 ? return ( x + y + z ) : return ( x * y * z ) ; 
return ( code == 1 ? x + y + z : x * y * z ) ;
ANSI V/s K & R 
calsum ( int x, int y, int z ) 
{ 
.. 
} 
calsum ( x, y, z ) 
int x, y, z ; 
{ 
.. 
} 
ANSI 
Kernighan & 
Ritchie
main( ) 
{ 
int y ; 
printf ( ”Enter year” ) ; 
scanf ( ”%d”, &y ) ; 
romanize ( y ) ; 
} 
romanize ( int yy ) 
{ 
int n, i ; 
n = yy / 1000 ; 
for ( i = 1 ; i <= n ; i ++ ) 
printf ( ”m” ) ; 
Decimal Roman 
1000 m 
500 d 
100 c 
50 l 
10 x 
5 v 
1 i 
} 
Roman Equivalent 
1998 m d c c c c l x x x x i i i 
1998 
v
A More General Call 
main( ) 
{ 
int y ; 
printf ( ”Enter year” ) ; 
scanf ( ”%d”, &y ) ; 
, 1000 , ’m’ 
y = romanize ( y romanise ( y, 1000, ’) ; 
m’ ) ; 
} 
romanize ( intyy , int j , char ch 
) 
{ 
int n, i ; 
n = yy / j ; 
for ( i = 1 ; i <= n ; i ++ ) 
printf ( ) ; 
} 
”%c”, ch 
return ( yy % j ) ;
main( ) 
{ 
... 
1998 md c c c c lx xx x v i ii 
, ’m’ 
y = romanise ( y, 1000 ) ; 
y = romanise ( y, 500, ’d’ ) ; 
y = romanise ( y, 100, ’c’ ) ; 
y = romanise ( y, 50, ’l’ ) ; 
y = romanise ( y, 10, ’x’ ) ; 
y = romanise ( y, 5, ’v’ ) ; 
romanise ( y, 1, ’i’ ) ; 
} 
romanize ( intyy ) 
{ 
} 
, int j, char ch 
int n, i ; 
n = yy / j ; 
for ( i = 1 ; i <= n ; i ++ ) 
printf ( ”%c”, ch ) ; 
return ( yy % j ) ; 
Works
Advanced Features of 
Functions 
 Returning a non-integer value 
 Call by value / Call by reference 
 Recursion
Returning a Non-Integer Value 
main( ) 
{ 
a = square ( 2.0 ) ; 
b = square ( 2.5 ) ; 
c = square ( 1.5 ) ; 
printf ( ” %f %f %f ”, a, b, c, ) ; 
square ( 2.0 ) ; 
} 
square ( f l o a t x ) 
{ 
float y ; 
y = x * x ; 
printf ( ” %f ”, y ) ; 
return ( y ) ; 
} 
Function 
Prototype 
4.0 6.0 2.0 
float a, b, c ; float square ( float ) ; 
4.0 
6.25 
2.25 
float square ( float x )
156 - 161 (e)
main( ) 
{ 
What’s Wrong? 
float square ( float ) ; 
float a = 0.5 ; 
f ( a ) ; 
} 
f ( float x ) 
{ 
float y ; 
y = square ( x ) ; 
printf ( ”%f”, y ) ; 
} 
float square ( float x ) 
{ 
return ( x * x ) ; 
}
What Would Be The Output 
main( ) 
{ 
int a = 10 ; 
printf ( ”n a = %d” , a ) ; 
a = f( ) ; 
printf ( ”n a = %d” , a ) ; 
} 
a = 10 
f( ) 
{ 
printf ( ”Hello !” ) ; 
} 
a = 45
main( ) 
{ 
Solution 1 
int a = 10 ; 
printf ( ”n a = %d” , a ) ; 
f( ) ; 
printf ( ”n a = %d” , a ) ; 
} 
a = 10 
f( ) 
{ 
printf ( ”Hello !” ) ; 
} 
a = 10 
Returned value is ignored.
main( ) 
{ 
Solution 2 
void f( ) ; 
int a = 10 ; 
printf ( ”n a = %d” , a ) ; 
f( ) ; 
printf ( ”n a = %d” , a ) ; 
} 
a = 10 
void f( ) 
{ 
printf ( ”Hello !” ) ; 
} 
a = 10 
void prevents returning of value.
Advanced Features of functions 
 Returning a non-integer value 
 Call by Value / Call by Reference 
 Recursion 

Pointers 
* 
* 
The Biggest 
Hurdle !!
Why Learn Pointers? 
 Simple 
 Powerful 
 Earn respect and money
10 4080 
&i 
&i 
*( ) 
Memory 
Address 
Reference 
Memory location 
Cell number 
main( ) 
{ 
Things Are Simple 
int i = 10 ; 
printf ( ”n value of i = %d 
” , i 
) ; 
printf ( ” n address of i = ” , ) ; 
%d 
printf ( ” n value of i = ” , ) ; 
} 
POINTER OPERATORS 
& - ‘Address Of’ Operator 
* - ‘Value at Address’ Operator 
- ‘Indirection’ Operator 
i 
4080 %d 10 
0 1 2 3 
10 
 

Would This Work... 
int i = 10 ; 
j = &i ; 
j = &23 ; 
j = & ( i + 34 ) ; 
& can be used 
only with a variable 
* can be used with 
variable, constant 
or expression 
printf ( “ %d ” *j ) ; 
printf ( “ %d ”, *4568 ) ; 
printf ( “ %d ”, * ( 4568 + 1 ) ) ; 
Some Change 
Necessary
The Next Step 
int j ; int k ; 
printf ( ” n value of i = %d ” , i ) ; 
printf ( ” n address of i = %d ” , &i ) ; 
printf ( ” n value of i = %d ” , *( &i ) ) ; 
j = &i ; 
printf ( ” n address of j = % d ” , & j ) ; 
printf ( ” n value of j = % d ” , ) ; 
printf ( ” n value of j = ” , ) ; 
%d 
j 
*&j 
10 4080 6010 
k = &j ; 
printf ( ” n %d %d %d %d ” , k , &k , *k , *&k ) ; 
printf ( ” n %d %d %d %d %d %d ”, 
) ; 
} 
i j k 
4080 6010 5112 
i, *&i, *j, **&j, 
**k, ***&k 
main( ) 
{ 
int i = 10 ; 
6010 
4080 
4080 
6010 5112 4080 6010 
* ** 
10 10 10 10 10 10
Kneel Down , Bow Your Head , 
Close Your Eyes ...
In Essence ... 
& 
* 
- Address of operator 
- value of address operator 
Pointer 
Operators 
variable  *& variable 
Pointers are variables which hold 
addresses of other variables 
? ? ? 
Integer Pointer 
int *j , ***l , ****m ; 
int i = 10 ; **k , 
m = &l ; 
j = &i ; k = &j ; l = &k ; 
int &k ; 
k = && j ;
Accessing Screen 
if ( >= ’A’ && *s <= ’Z’ ) 
*(( s ( + s i ) + i ) >= ’A’ && **(s s+<= i) ’Z’ <= ) 
’Z’ ) 
i)= *(s+i) + 32 ; 
’a’ && *(s+i) <= ’z’ ) 
**(s = s+*i) s = - *s 32 - ; 
32 ; 
main( ) 
{ 
while ( 1 ) 
{ 
} 
s = 1000 ; 
for ( i = 0 ; i <= 1999 ; i++ ) 
{ 
} 
* s 
*s = *s + 32 ; 
else 
{ 
if ( *s >= ’a’ && *s <= ’z’ ) 
} 
} 
0xB8000000 
int i ; char *s ; 
char far *s ; 
*(s+i) = *s + 32 ; 
*(s+i) >= *s <= ’z’ ) 
*(s+i) - 32 ; 
Screen 
M 
1000 1000 
+1 
1000 
+2 
1000 
+3 
1000 + 1999 
char ch ; 
ch = ’A’ ; 
ch = ch + 32 ;
Accessing Screen 
main( ) 
{ 
M 
+ 2 + 4 + 6 
0xB8000000 
+ 3998 
int i ; char far *s ; i += 2 
while ( 1 ) 
{ 
} 
s = 0xB8000000 ; 
for ( i = 0 ; i <= 1999 ; i++ ) 
{ 
} 
if ( * ( s + i ) >= ’A’ && *(s+i) <= ’Z’ ) 
*(s+i) = *(s+i) + 32 ; 
else 
{ 
if ( *(s+i) >= ’a’ && *(s+i) <= ’z’ ) 
*(s+i) = *(s+i) - 32 ; 
} 
} 
3999
main( ) 
{ 
char far *s ; int r ; 
char far * v ; 
s = 0xB8000000 ; 
for ( r = 1 ; r <= 24 ; r ++ ) 
{ 
v = s + r * 160 ; 
* v = ch ; 
} 
} 
Screen 
ch = * s ; 
0xB8000000 
+160 
+320 
+480 
+640 
M 
+2 +4 +6 
+3998 
To Make Characters Fall 
char ch ;
main( ) 
{ 
Make All Characters Fall 
char far *s ; int r ; 
char far *v ; 
s = 0xB8000000 ; 
int c ; 
char ch ; 
for ( c = 0 ; c <= 79 ; c++ ) 
} 
} 
ch = * s 
0xB8000000 Screen 
+160 
+320 
+480 
+640 
M 
+2 +4 +6 
+3998 
{ 
+ c * 2 ) ; 
( 
for ( r = 1 ; r <= 24 ; r++ ) 
{ 
v = s + r * 160 
+ c * 2 ; 
* v = ch ; 
}
Any Screen Address 
Column 20 
A 
0xB8000000 
Row 10 
v = 0xB8000000 + 10 * 160 + 20 * 2 ; 
In general, 
v =0xB8000000 + r * 160 + c * 2 ; 
*v = ch ;
main ( ) 
{ 
Bells And Whistles 
char far *s ; char ch ; 
char far *v ; int r, c ; 
s = 0xB8000000 ; 
for ( c = 0 ; c <= 79 ; c++ ) 
{ 
ch = *s ; 
for ( r = 1 ; r <= 24 ; r ++ ) 
{ 
Millisec 
v = s + r * 160 
+ c * 2 ; 
*v = ch ; 
} 
} 
} 
What if 10 - 20? 
delay ( 60 
) ; 
* ( v - 160 ) = ’ ’ ; 
sound ( 350 ) ; delay ( 100 ) ; 
nosound ( ) ; 
0xB8000000 Screen 
+160 
+320 
+480 
+640 
M 
+2 +4 +6 
+3998 
= * ( s + c * 2 ) ; 
r = random ( 2 4 ) ; 
c = random ( 79 ) ; 
Use 
# include ”time.h” randomize( )
Are They Same 
char far *s ; 
char far *v ; 
char far *s, *v ; 
char far *s, far *v ; 
 
 
char far *s ; 
s = 0xB8000000 ; 
char far *s = 0xB8000000 ;
Why Two Bytes Apart 
main( ) 
{ 
char far *s ; int i ; 
int color = 0 ; 
s = 0xB8000000 ; 
while ( 1) 
{ 
} 
} 
for ( i = 1 ; i <= 3999 ; i += 2 ) 
* ( s + i ) = color ; 
color++ ; 
if ( color > 255 ) 
color = 0 ; 
Screen 
VDU Memory 
ASCII Color 
Color Byte 
8 Bits 
Min value 
00000000 
Max value 
11111111 
Range 0 to 255 
M 
+2 +4 +6 
0xB8000000
Is The Caps Lock On 
main( ) 
{ 
kb = 0x417 ; 
*kb = 64 ; 
} 
char far *kb ; 
How would you put off 
the caps lock? 
Toggle Keys 
Caps lock 
Num lock 
Scroll lock 
Insert 
01000000 
7 6 5 4 3 2 1 
0x417 
0 
Caps Lock 
1 - On 
0 - Off
Don’t Do Delete 
00001100 
main( ) 
{ 
char far *kb ; 
kb = 0x417 ; 
‘ 
printf ( ”Please press 
delete key” ) ; 
*kb = 12 ; 
getch( ) ; 
} 
7 6 5 4 3 2 1 0 
Alt Ctrl 
Input Fun. 
scanf( ) 
getch( )
Happy Birthday Joshi 
7 6 5 4 3 2 1 0 
0 0 
Our Program 
Happy Birthday Joshi 
Del Ctrl + Alt + Del 
Ctrl + Alt + Del Del
What would be the output 
main( ) 
{ 
int i = 10 ; float a = 3.14 ; char ch = ’z’ ; 
j = &i ; b = &a ; dh = &ch ; 
k = &j ; c = &b ; eh = &dh ; 
printf ( ” %d %d %d 
”, j , b , dh ) ; 
printf ( ” ”, *k ,*c , *eh ) ; 
%d %d %d 
i 
a ch 
j b dh 
4000 5000 6000 
4000 5000 6000 
500 1500 2500 
1000 2000 3000 
printf ( ” ”, sizeof ( i ), sizeof ( a ), sizeof ( ch ) ) ; 
%d %d %d 
10 
3.14 z 
1000 2000 3000 
1000 2000 3000 
k c eh 
1000 2000 3000 
2 4 1 
Continued... 
int *j, **k ; float *b, **c ; char *dh, **eh ;
i 
a ch 
j b dh 
4000 5000 6000 
4000 5000 6000 
500 1500 2500 
2 2 2 
printf ( ” %d %d %d ”, sizeof ( j ), sizeof ( b ), sizeof ( dh ) ) ; 
printf ( ”%d %d %d”, sizeof ( k ), sizeof ( c ), sizeof ( eh ) ) ; 
printf ( ” %d %f %c ” , **k, **c, **eh ) ; 
} 
10 
3.14 z 
1000 2000 3000 
1000 2000 3000 
k c eh 
2 2 2 
10 3.14 z 
...Continued
What would be the output 
main( ) 
{ 
int i = 10 ; float a = 3.14 ; char ch = ’z’ ; 
j = &i ; b = &a ; dh = &ch ; 
k = &j ; c = &b ; eh = &dh ; 
printf ( ” %d %d %d 
” , j , b , dh ) ; 
printf ( ” ” , *k ,*c , *eh ) ; 
%d %d %d 
i 
a ch 
j b dh 
4000 5000 6000 
4000 5000 6000 
500 1500 2500 
1000 2000 3000 
printf ( ” ” , sizeof ( i ), sizeof ( a ), sizeof ( ch ) ) ; 
%d %d %d 
10 
3.14 z 
1000 2000 3000 
1000 2000 3000 
k c eh 
1000 2000 3000 
2 4 1 
Continued... 
int *j, **k ; float *b, **c ; char *dh, **eh ;
i 
a ch 
j b dh 
4000 5000 6000 
4000 5000 6000 
500 1500 2500 
2 2 2 
printf ( ”%d %d %d ”, sizeof ( j ), sizeof ( b ), sizeof ( dh ) ) ; 
printf ( ”%d %d %d”, sizeof ( k ), sizeof ( c ), sizeof ( eh ) ) ; 
printf ( ”%d %f %c” , **k, **c, **eh ) ; 
} 
10 
3.14 z 
1000 2000 3000 
1000 2000 3000 
k c eh 
2 2 2 
10 3.14 z 
...Continued
Truly Speaking 
main( ) 
{ 
int i = 25 ; 
float a = 3.14 ; 
j = &i ; 
int *j ; 
float *b ; 
b = &a ; 
} 
printf ( ”%d%f”, *j, *b ) ; 
i 
Binary of 25 401 
401 402 
a 
501 502 503 504 
j 
b 
Binary of 3.14 501
Accessing Individual Bytes 
main( ) 
{ 
float a = 3.14 ; 
float *b ; int *c ; char *d ; 
501 
503 504 
a 
502 
b c d 
b = &a ; 
c = &a ; 
d = &a ; 
printf ( ” %f %d %d 
”, *b, *c, *d ) ; 
printf ( ”%d %d %d %d”, *d, *(d+1), *(d+2), *(d+3) ) ; 
printf ( ”%d %d”, *( c+1 ), *( c+2 ) ) ; 
} 
501 
0’s & 
1’s 
0’s & 
1’s 
0’s & 
1’s 
0’s & 
1’s 
501 
501
main( ) 
{ 
int i = 25 ; int *j ; 
float a = 3.14 ; 
float *b ; 
char far *kb ; 
kb = 0x417 ; 
j = &i ; 
b = &a ; 
} 
Why far and near
i a j b kb 
25 3.14 200 300 
200 300 
0x417 
Code Segment 
Turbo C 
DOS/Windows 
BDA 0x417 
IVT 
0xB8000000 
Screen 
M A 
0xB8000000 
M 
A B C D E F 
384 KB 
High Memory 
640 KB 
Base or 
Conventional 
Memory 
 
Data 
Segment 
Free Memory 
(640 + 64 ) * 1024 
HEX 
64 * 6
main( ) 
{ 
Pointer Sizes 
int i = 25 ; int *j ; 
float a = 3.14 ; 
float *b ; 
char far *kb ; 
kb = 0x417 ; 
j = &i ; 
b = &a ; 
} 
2 2 
printf ( ”%d %d %d”, sizeof ( j ), sizeof ( b ), 
sizeof ( kb ) ) ; 
4
How Much Memory 
main( ) 
{ 
int far *m ; 
m = 0x413 ; 
printf ( ”%d” ,*m ) ; 
} 0x413 0x414 
640 
640 KB 
BDA 
Ideally 
632 
634 
636 
?
main( ) 
{ 
int a = 10, b = 20 ; 
printf ( ”%d%d”, a, b ) ; 
swapv ( a , b ) ; 
printf ( ”%d%d”, a, b ) ; 
} 
swapv ( int x, int y ) 
{ 
} 
int t ; 
t = x ; x = y ; y = t ; 
printf ( ”%d%d”, x, y ) ; 
10 20 
10 20 
a b 
x y t 
x y t 
10 20 g 
10 20 10 
x y t 
20 20 10 
x y t 
20 10 10 
20 10 
10 20
swapr ( & a , & b ) ; 
printf ( ”%d%d”, a, b ) ; 
swapr ( ) 
{ 
} 
int t ; 
t = *x ; 
*x = *y ; 
*y = t ; 
10 20 
20 10 
a b 
20 
20 10 
300 
x y t 
main( ) 
{ 
int a = 10, b = 20 ; 
printf ( ”%d%d”, a, b ) ; 
int *x , int *y 200 } 10 
200 300 10
main( ) 
{ 
int a = 10, b = 20, c = 30 
sumprod ( a, b, c, ) ; 
printf ( ”%d%d”, s, p ) ; 
} 
, s, p ; 
60 6000 
sumprod ( int x, int y, int z, ) 
{ 
} 
x + y + z ; 
x * y * z ; 
&s, &p 
int *ss, int*pp 
ss = 
pp = 
* 
* 
a b c s p 
6G0 60G00 
100 200 
10 20 30 
x y z ss pp 
10 20 30 100 200
Advanced Features of 
Functions 
• Returning Non-integer Value 
• Call By Value / Reference 
• Recursion
To Be A Successful Software 
Developer 
Creative Intelligence 
Analytical Ability 
Patience 
Ability To Think
Simpler Form 
main( ) 
{ 
printf ( ”Hi” ) ; 
main( ) ; 
}
One More Form 
main( ) 
{ 
f( ) ; 
} 
f( ) 
{ 
printf ( ”Hi” ) ; 
f( ) ; 
}
More General main( ) 
{ 
int num, sum ; 
printf ( ”Enter a number” ) ; 
scanf ( ”%d”, &num ) ; 
} 
sumdig ( int n ) 
{ 
int d ; int s = 0 ; 
while ( n != 0 
) 
{ 
d = n % 10 ; 
n = n / 10 ; s = s + d ; 
} 
return ( s ) ; 
} 
31698 
d5 
372 
d3 
2846 
d4 
n s d 
327 0 7 
32 7 2 
3 9 3 
0 12 
327 
sum = sumdig ( num ) ; 
printf ( ”%d”, sum ) ;
main( ) 
{ 
printf ( ”Enter a number” ) ; 
scanf ( ”%d”, &num ) ; 
} 
int num, sum ; 
rsum ( int n ) 
{ 
int d ; int s ; 
if ( ) 
{ 
n != 0 
d = n % 10 ; 
s = d + rsum ( n ) ; 
n = n / 10 ; 
} 
else 
return ( 0 ) ; 
} return ( s ) ; 
327 
sum = rsum ( num ) ; 
printf ( ”%d”, sum ) ;
rsum ( int n ) 
{ 
if ( ) 
{ 
n != 0 
d = 
s = 
rsum ( int n ) 
{ 
if ( ) 
{ 
n != 0 
d = 
n = 
s = 
} 
else 
return ( 0 ) ; 
return ( s ) ; 
} 
rsum ( int n ) 
{ 
if ( ) 
{ 
n != 0 
d = 
n = 
s = 
} 
else 
return ( 0 ) ; 
return ( s ) ; 
} 
rsum ( int n ) 
{ 
if ( ) 
{ 
n != 0 
d = 
s = 
n = 
} 
else 
return ( 0 ) ; 
return ( s ) ; 
} 
327 % 10 ; 
327 / 10 ; 
7 + rsum ( 32 ) ; 
n = 
} 
else 
return ( 0 ) ; 
return ( s ) ; 
} 
32 % 10 ; 
32 / 10 ; 
2 + rsum ( 3 ) ; 
3 % 10 ; 
3 / 10 ; 
3 + rsum ( 0 ) ; 
327
Factorial Value 
main( ) 
{ 
printf ( ”Enter a number” ) ; 
scanf ( ”%d”, &num ) ; 
} 
int num, fact ; 
fact = factorial ( num ) ; 
printf ( ”%d”, fact ) ; 
factorial ( int n ) 
{ 
int p = 1 ; 
while ( ) 
{ 
} 
n != 0 
p = p * n ; 
n-- ; 
return ( p ) ; 
}
Recursive Factorial 
main( ) 
{ 
printf ( ”Enter a number” ) ; 
scanf ( ”%d”, &num ) ; 
} 
int num, fact ; 
fact = refact ( num ) ; 
printf ( ”%d”, fact ) ; 
refact ( int n ) 
{ 
int p ; 
if ( n ! = 0 ) 
p = n * refact ( n - 1 ) ; 
return ( p ) ; 
} 
else 
return ( 1 ) ;
Advantages of Recursion 
Ease ? 
Speed ?  
 
 

Peg A Peg B Peg C A B C 
A to C 
A B C 
A B C 
A to B 
A B C A B C 
C to B A to C B to A 
B to C A to C 
3 ! + 1 
A B C A B C
Chapter 5 
Chapter 5
Data Types in C
int i ; 
short int i ; 
default 
signed short int i ; 
i 
Type of Integers 
int 
short int long int 
signed short unsigned short 
Specifier: %u 
Specifier: %i %d 
%o %x %X 
15 bits = value 
max value 
111 1111 1111 1111 
32767 
Sign bit 
0 - Positive 
1 - Negative 
Range:-32768 to 
+32767 
Size: 2 Bytes 
Range: 0 to 
65535 
Size: 2 Bytes
Type of Integers 
Sign bit 
0 - Positive 
int 1 - Negative 
111 1111 1111 1111 1111 1111 1111 1111 
short int long int 
signed unsigned 
Specifier: %ld 
long int a 
31 bits = value 
max value 
2147483647 
long signed long unsigned 
Specifier: %lu 
Range: -2147483648 to 
+2147483647 
Size: 4 Bytes 
Range: 0 to 
4294967295 
Size: 4 Bytes
Type of Chars 
char 
char ch ; 
Sign bit 7 bits = value 
0 - Positive 
1 - Negative 
signed char unsigned char 
Specifier: %c Specifier:%c 
max value 
111 1111 
127 
Range : -128 to + 127 
Size : 1 Byte 
Range: 0 to 255 
Size: 1 Byte
Types of Real 
Real 
float double long double 
Range: -3.4*1038 to 
+3.4*1038 
Size: 4 bytes 
Specifier: %f 
Range: -1.7*10308 
to +1.7*10308 
Size: 8 bytes 
Specifier: %lf 
Range: -1.7*104932 
to +1.7*104932 
Size: 10 bytes 
Specifier: %Lf 
1 - 8 - 23 1 - 11 - 52 1 - 15 - 64 
127 1023 16383
int 
short int long int 
signed unsigned signed unsigned 
char Real 
signed unsigned float double long double
Chars With a Sign? 
char ch = ’A’ ; 
ch 
65 
char dh = -15 ;  
dh 
-15
What If We Exceed The 
Range? 
signed char ch = 128 ; 
printf ( ”%d”, ch ) ; 
-128 
Why?
Why This Bias? 
char 
-128 +127 
int 
-32768 +32767 
long int 
-2147483648 +2147483647
signed char ch 
= -128 ; 
Binary of 128 
10000000 
1’s complement 
01111111 
2’s complement 
10000000 
printf ( ”%d”, ch ) ; 
-128 
unsigned char dh 
= 128 ; 
signed char eh 
= 128 ; 
Binary of 128 
10000000 
printf ( ”%u”, dh ) ; 
Binary of 128 
10000000 
Add 0 for Positive 
Sign bit 
010000000 
Store rightmost 
8 bits 
10000000 
printf ( ”%d”, eh ) ; 
01111111 
+1 
10000000 
-128 
128
Various Forms 
signed short int i ; 
short signed int i ; 
short int i ; 
signed int i ; 
int i ; 
short i ; 
signed i ; 
unsigned short int j ; 
short unsigned int j ; 
unsigned int j ; 
unsigned j ; 
long signed int k ; 
signed long int k ; 
long int k ; 
long k ; 
long unsigned int i ; 
unsigned long int i ; 
unsigned long i ; 
signed char ch ; 
char signed ch ; 
char ch ; 
unsigned char dh ; 
float a ; 
long double e ; 
char unsigned dh ; 
double d ;
What Is 365? 
int 365 
long int 
unsigned int 
unsigned long int 
365L 365l 
365u 
365lu 365ul 
What Is 3.14? 
double 3.14 
float 
long double 
3.14f 
3.14L
Would This Work 
main( ) 
{ 
int i ; 
  
for ( i = 0 ; i <= 255 ; i++ ) 
printf ( ”%d %c”, i, i ) ; 
} 
char i ; unsigned char i ;
Stick To Your Guns 
main( ) 
{ 
for ( i = 0 ; i < 255 
; i++ ) 
printf ( ”%d %c”, i, i ) ; 
} 
unsigned char i ; 
printf ( ”%d %c”, i, i ) ;
Storage Classes In C 
A Complete definition of variable : 
Type 
Storage Class
A Storage Class signifies 
Storage 
Default Initial Value 
Scope 
Life
Types of Storage Classes 
Automatic 
Register 
Static 
External
Automatic Storage Class 
Storage 
Default Initial Value 
Scope 
Life 
Memory 
Garbage 
Local to the block in which 
the variable is defined 
Till the control is in the 
block in which the 
variable is defined
Automatic Storage Class 
main( ) 
{ 
int a ; 
static int b ; 
automatic int c ; 
printf ( ”%d %d %d”, a, b, c ) ; 
automatic 
auto 
} 
Error 
? 
 

Default Initial Value 
main( ) 
{ 
int a ; 
static int b ; 
auto int c ; 
printf ( ”%d%d%d”, a, b, c ) ; 
} 
G 0 G
Which Is Correct? 
main( ) 
{ 
int a = 10 ; 
float a = 3.14 ; 
. . . . . . . 
. . . . . . . 
} 
main( ) 
{ 
int a = 10 ; 
int a = 20 ; 
. . . . . . . 
. . . . . . . 
 } 
 
Tip: Redefinition not allowed
Redefinition? 
main( ) 
{ 
int a = 10 ; 
{ 
int a = 20 ; 
{ 
int a = 30 ; 
printf ( ”%d”, a ) ; 
} 
printf ( ”%d”, a ) ; 
} 
printf ( ”%d”, a ) ; 
} 
Error ! Why? 
main ( ) 
{ 
int a = 20 ; 
f ( ) ; 
} 
f ( ) 
{ 
printf ( ”%d”, a ) ; 
} 
30 
20 
10
Death, But When? 
main( ) 
{ 
Won’t die when the 
control goes to f( ) 
int i =10, j = 20, k, l ; 
k = f ( ) ; 
l = i + j + k ; 
printf ( ”%d”, l ) ; 
} 
f ( ) 
{ 
int m = 5, n ; 
n = m * 2 ; 
return ( n ) ; 
} 
Would die when 
control goes back
Register Storage Class 
Storage : 
Default Initial value : 
Scope : 
Life : 
CPU Registers 14 Each of 2 bytes 
Central Processing 
Unit 
Microprocessor μp
Different Speeds? 
main( ) 
{ 
auto int i ; register int i ; 
for ( i = 1 ; i <= 250 ; i++ ) 
printf ( ”%d %c”, i, i ) ; 
} 
main( ) 
{ 
for ( i = 1 ; i <= 250 ; i++ ) 
printf ( ”%d %c”, i, i ) ; 
} 
Register Storage Class 
Storage : CPU Registers 
Default Initial Value : Garbage 
Scope : Local to the block in which the variable is defined 
Life : Till the control remains in the block in which 
the variable is defined
Be Judicious 
main( ) 
{ 
register int i ; register int j = 20 ; 
for ( i = 1 ; i <= 250 ; i++ ) 
printf ( ”%d %c”, i, i ) ; 
printf ( ”%d”, j ) ; 
}
Static Storage Class 
Storage 
Default Initial Value 
Scope 
Life 
Memory 
0 
Local to the block in which 
the variable is defined 
Variable persists between 
different function calls
main( ) 
{ 
increment( ) ; 
increment( ) ; 
increment( ) ; 
} 
increment( ) 
{ 
auto int i = 1 ; 
register int j = 1 ; 
static int k = 1 ; 
i++ ; 
j++ ; k++ ; 
printf ( ”%d %d %d”, i, j, k ) ; 
2 2 2 
2 2 3 
2 2 4 
}
When to Use Static 
main( ) 
{ 
f( ) 
f( ) ; 
p = 
int a = 20 ; 
return ( &a ) ; 
} 
printf ( ”%d”, *p ) ; 
} 
int *p ; int * f( ) ; 
{ 
a p 
20 250 
250 
int * f( ) char ** f ( int *, float * ) ; 
static int a = 20 ;
External Storage Class 
main( ) 
{ 
printf ( ”%d”, a ) ; 
increment( ) ; 
increment( ) ; 
decrement( ) ; 
printf ( ”%d”, a ) ; 
} 
increment( ) 
{ 
a++ ; printf ( ”%d”, a ) ; 
} 
decrement( ) 
{ 
a-- ; printf ( ”%d”, a ) ; 
} 
10 
11 
12 
11 
11 
int a = 10 ; 
Output
Declaration V/s Definition main( ) 
{ 
extern int a ; 
printf ( ”%d”, a ) ; 
increment( ) ; 
increment( ) ; 
decrement( ) ; 
printf ( ”%d”, a ) ; 
} 
increment( ) 
{ 
extern int a ; 
Declaration 
} a++ ; printf ( ”%d”, a ) ; 
decrement( ) 
{ 
extern int a ; 
} a-- ; printf ( ”%d”, a ) ; 
int a = 10 ; 
Declaration 
Declaration 
Definition 
float square ( float ) ; 
float square ( float ) 
{ 
.. 
.. 
} 
Definition
Declaration V/s Definition main( ) 
{ 
extern int a ; 
printf ( ”%d”, a ) ; 
increment( ) ; 
increment( ) ; 
decrement( ) ; 
printf ( ”%d”, a ) ; 
} 
increment( ) 
{ 
extern int a ; 
a++ ; printf ( ”%d”, a ) ; 
int a = 10 ; 
decrement( ) 
{ 
extern int a ; 
} 
a-- ; printf ( ”%d”, a ) ; 
}
Declaration V/s Definition main( ) 
{ 
extern int a ; 
printf ( ”%d”, a ) ; 
increment( ) ; 
increment( ) ; 
decrement( ) ; 
printf ( ”%d”, a ) ; 
int a = 10 ; 
increment( ) 
{ 
extern int a ; 
decrement( ) 
{ 
extern int a ; 
} 
a++ ; printf ( ”%d”, a ) ; 
} 
a-- ; printf ( ”%d”, a ) ; 
}
Two Types Of Conflicts 
int a = 10 ; 
main( ) 
{ 
int a = 20 ; 
{ 
int a = 30 ; 
printf ( ”%d”, a ) ; 
} 
printf ( ”%d”, a ) ; 
} 
printf ( ”%d”, a ) ; 
30 
20
External Storage Class 
Storage 
Default Initial Value 
Scope 
Life 
Memory 
0 
Global 
Till execution of the 
program doesn’t end
Which is The Most Powerful 
 Automatic 
 Register 
 Static 
 External 
All other cases 
For frequently 
used variables 
If variable is to live 
across function calls 
If variable required by 
all functions
Ctrl F9 
F9 
F7 
F8 
C Preprocessor 
Compile and Execute 
Compile 
Step into 
Step over
main( ) 
{ 
int a = 10 ; 
float b = 3.5 ; 
display( ) ; 
printf ( ”Hello” ) ; 
} 
display( ) 
{ 
printf ( ”Nagpur” ) ; 
}
A Closer Look 
Hand written program 
Text Editor 
C Source Code 
Preprocessor 
Expanded source code 
Compiler 
Object code 
Linker 
Executable code 
C : 
Helps in typing 
TC 
WS4 . . 
a program 
WORKS 
Expands the 
source code 
Converts expanded source 
code into machine language
Why IDE 
 Editor 
 Preprocessor 
 Compiler 
 Linker 
Ctrl F9 
Turbo C/C++ 
Preprocesses, 
Compiles, Links 
and Executes
Types of Preprocessor Directives 
File Inclusion 
Macro Expansion 
Conditional Compilation 
Miscellaneous Directives
Preprocessor In Action 
# include ”goto.c” 
main( ) 
{ 
Compiler 
clrscr( ) ; 
gotorc ( 10, 20 ) ; 
printf ( ”Hello !” ) ; 
} 
Source Code 
Ctrl F9 
Preprocessor 
main( ) 
{ 
clrscr( ) ; 
gotorc ( 10, 20 ) ; 
printf ( ”Hello !” ) ; 
} 
gotorc( - , - ) 
{ 
--- 
} 
Expanded source code
Unresolved Externals 
gotorc( - , - ) 
{ 
main( ) 
{ 
--- 
} 
--- 
} Compiler 
machine language 
code of gotorc( ) 
machine language 
code of main( ) 
Error Why?
Solution 
m/c language code 
of gotorc( ) 
m/c language code 
of main( ) 
m/c language code 
of clrscr( ) 
m/c language code 
of printf( ) 
Linker 
Executable code
Your wish 
# include ”goto.c” 
# include <goto.c> 
C : 
TC 
WS4 . . 
WORKS 
INCLUDE 
INCLUDE DIRECTORIES 
C: TC INCLUDE 
C: TC LIB 
C: TC WORKS 
C: TC 
LIBRARY DIRECTORIES 
OUTPUT DIRECTORY 
SOURCE DIRECTORIES
Chapter 6 
Chapter 6
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 = 1 ; i <= 10 ; i++ )
Macro Expansion 
# define PI 3.14 
main( ) 
{ 
float r, a ; 
printf ( ”Enter radius” ) ; 
scanf ( ”%f”, &r ) ; 
a = PI * r * r ; 
printf ( ”n%f”, a ) ; 
} 
a = 3.14 * r * r ; 
3.141528 
  
# define 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 ) ; 
Order is 
unimportant 
float area ( float rr ) ; 
} 
float area ( float rr ) 
{ 
return ( PI * rr * rr ) ; 
} 
a = 3.14 * r * r ; 
Macros - Faster 
Functions - Less Space
Macros With Arguments 
# define S ( x ) x * x 
main( ) 
{ 
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 ) ; 
} 
j = 2 + 2 * 2 + 2 ; 
k = 3 + 1 * 3 + 1 ; 
i = 1 + 3 * 1 + 3 ; 
i = 4 * 4 ; 
m = ++n * ++n ; 
16 8 7 7 4 16
Conditional Compilation 
main( ) 
{ 
scanf ( .. ) ; /* input */ 
.. 
.. /* formula */ 
.. 
} 
.. 
.. 
.. 
/* 
*/
Conditional Compilation 
main( ) 
{ 
scanf ( .. ) ; /* input */ 
.. 
.. /* formula */ 
.. 
} 
.. 
.. 
.. 
# ifdef YES 
# endif 
2 Solutions : 
- # define YES 
- Delete # ifdef, # endif
Miscellaneous Directives 
# define YES 10 Redefinition 
main( ) 
{ 
a = YES - .. ; 
printf ( ”YES” ) ; 
} 
f( ) 
{ 
int YES ; 
} 
a = YES + .. ; 
# define YES 20 
# undef YES 
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 
10 %? 
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 
27 / 32
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 
per ( i ) per [ i ] 
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 
6 17 13 12 2 
2 17 13 12 6 
2 1 - 2 
2 
2 
2 6 
2 6 
2 6 12 
0 - 2 
0 - 3 
0 - 4 
13 17 12 6 
12 17 13 6 1 - 3 
6 17 13 12 1 - 4 
13 17 12 
12 17 13 
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
main( ) 
{ 
int a[ ] = { 7, 9, 16, -2, 8 } ; 
a[0] a[1] a[2] a[3] a[4] 
7 9 16 -2 8 
102 104 106 108 110 
int i ; 
printf ( ”%u %u %u”, &a[ 0 ], &a[ 1 ], &a[ 2 ] ) ; 
printf ( ”%u %u %u”, a, a + 1, a + 2 ) ; 
printf ( ”%d %d %d”, *a, *( a +1 ), *( a + 2 ) ) ; 
for ( i = 0 ; i <= 4 ; i ++ ) 
printf ( ”%d”, *( a + i ) ) ; 
for ( i = 0 ; i <= 4 ; i ++ ) 
printf ( ”%d”, a[ i ] ) ; 
} 
102 104 106 
102 104 106 
7 9 16 
Another Form... 
Subscript 
Notation 
Pointer 
Notation
More Forms... 
main( ) 
{ 
int a[ ] = { 7, 9, 16, -2, 8 } ; 
int i ; 
for ( i = 0 ; i <= 4 ; i ++ ) 
printf ( ”%d”, a[ i ] ) ; 
for ( i = 0 ; i <= 4 ; i ++ ) 
printf ( ”%d”, *( a + i ) ) ; 
for ( i = 0 ; i <= 4 ; i ++ ) 
printf ( ”%d”, *( i + a ) ) ; 
for ( i = 0 ; i <= 4 ; i ++ ) 
printf ( ”%d”, i[ a ] ) ; 
} 
a[ i ]i[ a ] 
*( a + i ) *( i + a )
Flexible Arrays 
int a[ n ] ; 
scanf ( ”%d”, &n ) ; 
Size of an array must always be mentioned 
as a positive, non-zero, integer constant
Pointer Arithmetic 
a 
b 
1008 2009 6002 
1012 
main( ) 
{ 
float a = 3.14 
, *b ; 
char ch = ’z’ 
, *dh ; 
int i = 25 
, *j ; 
b = &a ; dh = &ch ; j = &i ; 
printf ( ”%u %u %u”, b, dh, j ) ; 
b++ ; dh++ ; j++ ; 
printf ( ”%u %u %u”, b, dh, j ) ; 
b += 3 ; dh += 8 ; j -= 3 ; 
printf ( ”%u %u %u”, b, dh, j ) ; 
} 
3.14 
ch i 
z 25 
1008 2009 6002 
1008 
dh j 
2009 6002 
1024 
2010 6004 
2018 5998
Legal Pointer Arithmetic 
Pointer + number 
Pointer - number 
Pointer - Pointer 
Pointer 
Pointer 
Number
Access Using Pointers 
main( ) 
{ 
int a[ ] = { 7, 9, 16, -2, 8 } ; 
int *p ; int i ; 
p = a ; /* same as &a[ 0 ] */ 
printf ( ”%d”, *p ) ; p++ ; 7 
printf ( ”%d”, p ) ; 104 
printf ( ”%d”, *p ) ; 
9 
for ( i = 0 ; i <= 4 ; i++ ) 
printf ( ”%d”, *p ) ; 
p++ ; 
{ 
} 
} 
a[0] a[1] a[2] a[3] a[4] 
7 9 16 -2 8 
102 104 106 108 110 
p p 
102 
104 
p++ 
* p 
Ist 
* ++p ; 
IInd Ist 
p++ ; 
++ IInd 
++ *p ; 
p = a ; 

main( ) 
{ 
[ ] For Notation... 
int a[ ] = { 7, 9, 16, -2, 8 } ; 
int *p ; int i ; 
p = a ; 
for ( i = 0 ; i <= 4 ; i ++ ) 
printf ( ”%d %d %d %d”, 
} 
a[0] a[1] a[2] a[3] a[4] 
7 9 16 -2 8 
102 104 106 108 110 
*( p + i ), *( i + p ), 
p[ i ], i[ p ] ) ; 
int a[5] ; int 5[a] ; 
printf ( ”%d”, a[2] ) ; printf ( ”%d”, 2[a] ) ;  

Dancing Dolls Revisited 
main( ) 
{ 
char far *s = 0xB8000000 ; int i ; 
for ( i = 0 ; i <= 3999 ; i += 2 ) 
{ 
if ( *( s + i ) >= ’A’ && *( s + i ) <= ’Z’ ) 
} 
} 
s[ i ] 
* ( i + s ) 
i[ s ] 
a[i] 
- One is pointer 
or array 
- Other is int
main( ) 
{ 
Changing Array Address 
int a[ ] = { 7, 9, 16, -2, 8 } ; 
int *p ; int i ; 
p = a ; 
for ( i = 0 ; i <= 4 ; i ++ ) 
printf ( ”%d”, *p ) ; 
p++ ; 
{ 
} 
} 
for ( i = 0 ; i <= 4 ; i ++ ) 
printf ( ”%d”, *a ) ; 
a++ ; 
{ 
} Error 
a[0] a[1] a[2] a[3] a[4] 
7 9 16 -2 8 
102 104 106 108 110 
a-- ; 
a = a + 2 ; 
a = a - 2 ; 
a += 2 ; 
a -= 2 ;
main( ) 
{ 
Passing Array Elements 
int a[ ] = { 7, 9, 16, -2, 8 } ; 
int i ; 
display ( a[ 0 ], a[ 1 ], a[ 2 ], a[ 3 ], a[ 4 ] ) ; 
for ( i = 0 ; i <= 4 ; i++ ) 
display1 ( a[ i ] ) ; 
} 
display( int i, int j, int k, int l, int m 
) 
{ 
printf ( ”%d %d %d %d %d”, i, j, k, l, m ) ; 
} 
display1 ( int n 
) 
{ 
printf ( ”%d”, n ) ; 
} 
Which 
is good?
main( ) 
{ 
Passing Entire Array 
int a[ ] = { 7, 9, 16, -2, 8 } ; 
display2 ( a ) ; 
display3( a , sizeof ( a ) / 2 - 1 
) ; 
} 
display2 ( i n t * p ) 
int i ; 
for ( i = 0 ; i <= 4 ; i ++ ) 
printf ( ”%d”, ) ; 
} 
*( p + i ) 
{ 
display3 ( i n t * p , int n 
) 
{ 
i <= n 
int i ; 
for ( i = 0 ; i <= 4 ; i ++ ) 
} printf ( ”%d”, * ( p + i ) ) ; 
a[0] a[1] a[2] a[3] a[4] 
7 9 16 -2 8 
102 104 106 108 110 
qsort ( a, 5, , , ) ;
Remember... 
Any time an entire array is to be passed 
to function, it is necessary to pass 
(1) Base address of array 
(2) No. of elements present in the array
main( ) Two Dimensional Array 
{ int a[ ][ ] = { 
{ 2, 6, 1, 8, 4 } 
{ 1, 2, 5, 6, 8 } 
{ 7, 9, 8, 7, 21 } 
{ 4, 5, 6, 8, 10 } 
} ; 
optional 
int i, j ; 
printf ( ”%d”, a[ ][ ] ) ; 
2 4 
printf ( ”%d %d”, sizeof ( a ), a ) ; 
for ( i = 0 ; i <= 3 ; i++ ) 
{ 
for ( j = 0 ; j <= 4 ; j++ ) 
printf ( ”%d”, a [ i ][ j ] ) ; 
} printf ( ”n” ) ; 
} 
5 
optional 
int b[ ][1][2][3] 
compulsory 
21 
40 4080 
optional 
, 
, 
, 
compulsory 
compulsory 
Exception
main( ) 
{ 
int a[ ][ ] = { 
Find Biggest... 
,, 
7, 2, 6, 1 
3, 5, 4, 8 
6, 2, 9, 50 
1, 2, 3, 8 
} ; 
, 
int i, j, big ; int r, c ; 
big = a[ 0 ][ 0 ] ; 
for ( i = 0 ; i <= 3 ; i++ ) 
{ for ( j = 0 ; j <= 3 ; j++ ) 
{ 
if ( a[ i ][ j ] > big ) 
big = a[ i ][ j ] ; 
{ 
r = i ; c = j ; 
} 
} 
} 
printf ( ”%d ”, big ) ; printf ( ”%d %d ”, r, c ) ; 
} 
4 
r = 0 ; c = 0 ; 
Find Second Biggest 
and its Position
main( ) 
{ 
int a[ ][ ] = { 
4 Row Major 
7, 2, 6, 1, 
9, 3, 4, 5, 
10, 12, 16, 18 
} ; 
7 2 6 1 9 3 4 5 10 12 16 18 
502 504 506 508 510 512 514 516 518 520 522 524 
printf ( ”%d ”, a ) ; 
printf ( ”%d ”, *a ) ; 
printf ( ”%d %d %d ”, a+0 , a+1 , a+2 ) ; 
printf ( ”%d %d %d ”, *(a+0) , *(a+1) , *(a+2) ) ; 
printf ( ”%d %d %d ”, a[0] , a[1] , a[2] ) ; 
printf ( ”%d %d %d ”, a[0]+1 , a[1]+2 , a[2]+3 ) ; 
printf ( ”%d %d %d ”, *(a[0]+1) , *(a[1]+2) , 
*(a[2]+3) ) ; 
printf ( ”%d %d %d ”, a[0][1] , a[1][2] , a[2][3] ) ; 
502 
 
7 
 502  504  
506 
 7  2  
6 
 7  2  
6 
8  4  9 
 
Garbage/ 
Error 
2 4 } 18
int a[ ][ 4 ] = { 
7, 2, 6, 1 , 
9, 3, 4, 5, 
10, 12, 16, 18 
} ; 
7 2 6 1 9 3 4 5 10 12 16 18 
502 510 518 
printf ( ”%d”, a ) ; 
printf ( ”%d”, *a ) ; 
502 
int b[ ] = { 7, 2, 6, 1 } ; 
7 2 6 1 
402 
printf ( ”%d”, b ) ; 
printf ( ”%d”, *b ) ; 
402 
7 
502 
5 
int i = 5 ; 
printf ( ”%d”, i ) ; 
i 
5 
200
main( ) 
{ 
int a[ ][ ] = { 
4 Row Major 
7, 2, 6, 1, 
9, 3, 4, 5, 
10, 12, 16, 18 
} ; 
7 2 6 1 9 3 4 5 10 12 16 18 
502 510 514 518 524 
printf ( ”%d ”, a ) ; 
printf ( ”%d ”, *a ) ; 
printf ( ”%d %d %d ”, a+0 , a+1 , a+2 ) ; 
printf ( ”%d %d %d ”, *(a+0) , *(a+1) , *(a+2) ) ; 
printf ( ”%d %d %d ”, a[0] , a[1] , a[2] ) ; 
printf ( ”%d %d %d ”, a[0]+1 , a[1]+2 , a[2]+3 ) ; 
printf ( ”%d %d %d ”, *(a[0]+1) , *(a[1]+2) , 
*(a[2]+3) ) ; 
printf ( ”%d %d %d ”, a[0][1] , a[1][2] , a[2][3] ); 
502 
502 
502 510 518 
502 510 518 
502 510 518 
504 514 524 
2 4 18 
2 4 } 18
Moral... 
a[2][3] * ( a[2] + 3 ) * ( * ( a + 2 ) + 3 ) 
Array 
1-D 
2-D 
3-D 
4-D 
Subscript Pointer 
a[ i ] *( a+i ) 
a[ i ][ j ] *( *( a+i ) + j ) 
a[ i ][ j ][ k ] *( *( *(a + i ) + j ) + k ) 
a[ i ][ j ][ k ][ l ] *( *( *( *(a + i ) + j ) + k )+ l )
3 Ways 
main( ) 
{ 
int a[ ][ 4 ] = { 
7, 2, 6, 1, 
9, 3, 4, 5, 
10, 12, 16, 18 
} ; 
int i, j ; 
for ( i = 0 ; i <= 2 ; i++ ) 
{ 
for ( j = 0 ; j <= 3 ; j++ ) 
printf 
} 
} 
a[i][j][k] 
*( a[i][j] + k ) 
*( *( a[i] ) + j ) + k ) 
*( *( *(a + i ) + j ) + k ) 
( ”%d %d %d”, a[ i ][ j ] , 
*( a[ i ] + j ) , *( *( a + i ) + j ) ) ;
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY  M V B REDDY

More Related Content

What's hot

C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshopneosphere
 
C Programming basics
C Programming basicsC Programming basics
C Programming basicsJitin Pillai
 
T03 a basicioprintf
T03 a basicioprintfT03 a basicioprintf
T03 a basicioprintfteach4uin
 
PL SQL Quiz | PL SQL Examples
PL SQL Quiz |  PL SQL ExamplesPL SQL Quiz |  PL SQL Examples
PL SQL Quiz | PL SQL ExamplesShubham Dwivedi
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy BookAbir Hossain
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up ParsingGerwin Ocsena
 
An Expressive Model for Instance Decomposition Based Parallel SAT Solvers
An Expressive Model for Instance Decomposition Based Parallel SAT SolversAn Expressive Model for Instance Decomposition Based Parallel SAT Solvers
An Expressive Model for Instance Decomposition Based Parallel SAT SolversTobias Philipp
 
C C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.inC C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.inTIB Academy
 
Top down and botttom up 2 LATEST.
Top down     and botttom up 2 LATEST.Top down     and botttom up 2 LATEST.
Top down and botttom up 2 LATEST.Gerwin Ocsena
 
เขียนโปรแกรมใช้คำสั่ง Printf scanf
เขียนโปรแกรมใช้คำสั่ง  Printf scanfเขียนโปรแกรมใช้คำสั่ง  Printf scanf
เขียนโปรแกรมใช้คำสั่ง Printf scanfธงชัย พาศรี
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing TutorialMahira Banu
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CSowmya Jyothi
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiConstants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiSowmyaJyothi3
 

What's hot (20)

C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshop
 
C Programming basics
C Programming basicsC Programming basics
C Programming basics
 
C tutorial
C tutorialC tutorial
C tutorial
 
T03 a basicioprintf
T03 a basicioprintfT03 a basicioprintf
T03 a basicioprintf
 
Vcs4
Vcs4Vcs4
Vcs4
 
PL SQL Quiz | PL SQL Examples
PL SQL Quiz |  PL SQL ExamplesPL SQL Quiz |  PL SQL Examples
PL SQL Quiz | PL SQL Examples
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy Book
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
An Expressive Model for Instance Decomposition Based Parallel SAT Solvers
An Expressive Model for Instance Decomposition Based Parallel SAT SolversAn Expressive Model for Instance Decomposition Based Parallel SAT Solvers
An Expressive Model for Instance Decomposition Based Parallel SAT Solvers
 
C C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.inC C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.in
 
Top down and botttom up 2 LATEST.
Top down     and botttom up 2 LATEST.Top down     and botttom up 2 LATEST.
Top down and botttom up 2 LATEST.
 
SQL MCQ
SQL MCQSQL MCQ
SQL MCQ
 
เขียนโปรแกรมใช้คำสั่ง Printf scanf
เขียนโปรแกรมใช้คำสั่ง  Printf scanfเขียนโปรแกรมใช้คำสั่ง  Printf scanf
เขียนโปรแกรมใช้คำสั่ง Printf scanf
 
C tutorial
C tutorialC tutorial
C tutorial
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing Tutorial
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
COMPUTER PROGRAMMING
COMPUTER PROGRAMMINGCOMPUTER PROGRAMMING
COMPUTER PROGRAMMING
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
C
CC
C
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiConstants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya Jyothi
 

Viewers also liked

C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageRakesh Roshan
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language CourseVivek chan
 
Types and components of computer system
Types and components of computer systemTypes and components of computer system
Types and components of computer systemmkhisalg
 
Fundamentals Of Computer
Fundamentals Of ComputerFundamentals Of Computer
Fundamentals Of ComputerJack Frost
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programmingavikdhupar
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGAbhishek Dwivedi
 

Viewers also liked (9)

C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C language
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
Computer fundamental
Computer fundamentalComputer fundamental
Computer fundamental
 
Router commands
Router commandsRouter commands
Router commands
 
C language ppt
C language pptC language ppt
C language ppt
 
Types and components of computer system
Types and components of computer systemTypes and components of computer system
Types and components of computer system
 
Fundamentals Of Computer
Fundamentals Of ComputerFundamentals Of Computer
Fundamentals Of Computer
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programming
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMING
 

Similar to C SLIDES PREPARED BY M V B REDDY

Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C ProgrammingAniket Patne
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C BasicsBharat Kalia
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C ProgrammingMOHAMAD NOH AHMAD
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 FocJAYA
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview QuestionsGradeup
 
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptxINDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptxAbhimanyuChaure
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxAnkitaVerma776806
 
C prog ppt
C prog pptC prog ppt
C prog pptxinoe
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxKrishanPalSingh39
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02CIMAP
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingAlpana Gupta
 
Programming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxProgramming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxSONU KUMAR
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structuresPradipta Mishra
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxKrishanPalSingh39
 

Similar to C SLIDES PREPARED BY M V B REDDY (20)

Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
 
C language
C languageC language
C language
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptxINDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
C prog ppt
C prog pptC prog ppt
C prog ppt
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
 
Unit i intro-operators
Unit   i intro-operatorsUnit   i intro-operators
Unit i intro-operators
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Programming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxProgramming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptx
 
c_tutorial_2.ppt
c_tutorial_2.pptc_tutorial_2.ppt
c_tutorial_2.ppt
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structures
 
C Programming
C ProgrammingC Programming
C Programming
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptx
 
dinoC_ppt.pptx
dinoC_ppt.pptxdinoC_ppt.pptx
dinoC_ppt.pptx
 

More from Malikireddy Bramhananda Reddy

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 REDDYMalikireddy Bramhananda Reddy
 
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 REDDYMalikireddy Bramhananda Reddy
 
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 ReddyMalikireddy Bramhananda Reddy
 
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 orderMalikireddy Bramhananda Reddy
 

More from Malikireddy Bramhananda Reddy (20)

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
 
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
 
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 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
 
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
 
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
 
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
 
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 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
 
C LANGUAGE NOTES
C LANGUAGE NOTESC LANGUAGE NOTES
C LANGUAGE NOTES
 
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
 
Vcs29
Vcs29Vcs29
Vcs29
 
Vcs28
Vcs28Vcs28
Vcs28
 
Vcs26
Vcs26Vcs26
Vcs26
 
Vcs24
Vcs24Vcs24
Vcs24
 
Vcs23
Vcs23Vcs23
Vcs23
 
Vcs22
Vcs22Vcs22
Vcs22
 
Vcs21
Vcs21Vcs21
Vcs21
 

C SLIDES PREPARED BY M V B REDDY

  • 1. Why C?  C is simple  C is small  C is fast  C offers better interaction with hardware
  • 3. Historical Development of C 1960 International Committee 1963 National Committee 1967 1 Man Committee ALGOL 60 CPL Martin Richards - BCPL 1970 Ken Thompson, AT & T Bells Lab 1972 Dennis Ritchie, AT & T Bells Lab B C
  • 4. Where C Stands Computer Languages LLL HLL Machine Oriented Problem Oriented Better Machine efficiency Better Programming efficiency Ex. Assembly, Machine Ex. Basic, Fortran, Pascal,Cobol
  • 5. In The Beginning... English C Alphabets, Digits Words, Numbers Sentences Paragraph Alphabets, Digits, Sp. Symbols Constants, Variables, Keywords Statements or Instructions Program
  • 6. Alphabets, Digits, Special Symbols Alphabets A - Z, a - z Digits 0 - 9 Sp. Symbols Total - 32
  • 7. Constants and Variables Variables 3 x + 2 y = 20 Constants
  • 8. C Constants Primary Secondary Pointer Integer Real Character Array String Structure Union Enum etc.
  • 9. Integer Constants Ex. 421 -62 +45 4096 Rules:   1. No decimal point. 72 72.0 2. May be +ve or -ve. Default: +ve 32,500 4 7 3   3. No comma or spaces 4. Valid Range: -32768 to +32767
  • 10. Real Constants Ex. 427.62 +24.297 -0.00254 Rules: 1. Must contain a decimal point. 2. May be +ve or -ve. Default: +ve 3. No comma or spaces. 4. Valid Range: -3.4x 1038 to +3.4 x 1038
  • 11. Forms of Real Constants 427.62 +24.295 -0.00254 Fractional Form 4.2762E2 2.4295e1 -2.54e-3 Exponential Form E 4.2762 2 Mantissa Exponent
  • 12. Character Constants Ex. ’A’ ’m’ ’3’ ’+’ Rule : A single character enclosed within a pair of ’ ’. Are They OK? ‘Z’ ’Nagpur’ 
  • 13. C Variables Primary Secondary Pointer Array String Structure Union Enum etc. Integer Real Character
  • 14. Variables How many types? As many as the types of constants.  Is it necessary to identify types? Yes. Why? Why?
  • 15. What Happens in Memory... y x z x = 3 Memory 4 3 7 y = 4 z = x + y
  • 16. So A Variable is...  An entity whose value can change  A name given to a location in memory
  • 17. How to Identify Types 3 3.0 ’3’ a b c Integer Constant Real Constant Character Constant ? ? ?
  • 18. Different Languages, Different Rules BASIC FORTRAN i...n - Integer a - h, o - z - Real a% - Integer b! - Real c$ - Character
  • 19. C’s Way of Identifying Variables inta floatb char c a = 3 b = 3.0 c = ’3’
  • 20. Rules for Building Variable Names  First character must be an alphabet, rest can be alphabets, digits, or underscores. Ex. pop98, si_int _ - si-int si_int  Length <= 8 (Usually)  No commas or spaces  Variable names are case sensitive Ex. abc ABC Abc aBc AbC
  • 21. C Keywords  How many?  What are Keywords?  What are Reserved words?
  • 22. Would This Work? integer a real b character c    And How About This... int float float char float = 3 char = 3.14
  • 23. Where Do We Stand? C Alphabets, Digits, Sp. Symbols Constants, Variables, Keywords Statements or Instructions Program
  • 24. The First C Program p = 1000.50 n = 3 r = 15.5 si = p * n * r / 100
  • 25. Declaring Variables... float p, r, si int n p = 1000.50 n = 3 r = 15. 5 si = p * n * r / 100 Tip1: All variables must be declared.
  • 26. Printing Values... float p, r, si int n p = 1000.50 n = 3 r = 15. 5 si = p * n * r / 100 printf ( ”%f”, si )
  • 27. Terminology Matters ( ) Parentheses { } Braces [ ] Brackets
  • 28. General Form of printf( ) printf ( ”format string”, list of variables ) can contain %i - integer %f - float %c - character Ex. printf ( ”%f %f %f %i”, p, r, si, n )
  • 29. Statement Terminators I am a boy I go to school float p, r, si ; int n ; float p, r, si int n Statement Terminator : colon ; semicolon
  • 30. Free Form Language float p, r, si ; int n ; is same as float p, r, si ; int n ;
  • 31. What To Execute main( ) Collective Name float p, r, si ; int n ; p = 1000.50 ; n = 3 ; r = 15. 5 ; si = p * n * r / 100 ; printf ( ”%f”, si ) ;
  • 32. What Belongs to main( ) main( ) { float p, r, si ; int n ; p = 1000.50 ; n = 3 ; r = 15. 5 ; si = p * n * r / 100 ; printf ( ”%f”, si ) ; }
  • 33. Comments Are Useful /* Calculation of simple interest */ main( ) { float p, r, si ; int n ; p = 1000.50 ; n = 3 ; r = 15. 5 ; si = p * n * r / 100 ; printf ( ”%f”, si ) ; } Note Remark
  • 34. Tips About Comments  Any number of comments anywhere   Multiline comments - /* ............................. ............................... */  Nested comments -  /* .............. /* ....... */ ........ */
  • 35. A More General Program /* Calculation of simple interest */ main( ) { float p, r, si ; int n ; 1000.50 3 15 printf ( ”Enter values of p, n and r : ” ) ; scanf ( ”%f %i %f”, & p, & n, & r ) ; si = p * n * r / 100 ; ‘Address of ’ printf ( ”%f”, si ) ; operator }
  • 36. Try Your Hand At... Page No. 34 - 35 [G] (a) to (f)
  • 37. C Compilers  Turbo C  Quick C  Microsoft C  Aztech C  Zortech C  Lattice C  Watcom C  GreenLeaf C  Vitamin C All Provide an IDE 
  • 38. Integrated Development Environment Editor I D E Compiler Debugger Linker Preprocessor
  • 39. Editor and Compiler  Editor - Helps in typing / editing of a program  Compiler - Converts C language program to machine language program
  • 40. Editing Commands Cursor Movement Deletion  Up Arrow Del  Down Arrow  Right Arrow  Left Arrow Home End PgUp PgDn Ctrl+Home Ctrl+End Ctrl+PgUp Ctrl+PgDn Backspace Ctrl+T Ctrl+Y Computers are fun Screen File
  • 41. Some More Commands File Menu Miscellaneous F2 Save Ctrl+F9 Compile Alt+F5 Output- Screen New Save Save As Open Exit Dos Shell Permanent Temporary Tip: Use meaningful filenames. Ex. CH1PR1.C
  • 42. Interchanging Contents of Two Variables main( ) { printf ( ”Enter values of c and d” ) ; c d 5 10 c d 10 10 c d 10 10 int c, d ; scanf ( ”%i%i”, &c, &d ) ; c = d ; d = c ; 5 10 printf ( ”%i %i”, c, d ) ; } 10 10
  • 43. Interchanging Contents of Two Variables main( ) { int c, d , t ; printf ( ”Enter values of c and d” ) ; c d 5 10 t x c d 5 10 t 5 c d 10 10 t 5 c d 10 5 t 5 scanf ( ”%i%i”, &c, &d ) ; t = c ; c = d ; d = t ; printf ( ”%i %i”, c, d ) ; } 5 10 10 5
  • 44. One More Way scanf ( ”%i%i”,&c, &d ) ; c d 25 10 c d 25 15 c d 10 15 c = c + d ; d = c - d ; c = c - d ; 15 10 c d 15 10
  • 45. Sum of Digits main( ) { printf ( ”Enter a five digit no.” ) ; scanf ( ”%d”, &n) ; 2 6 9 1 3 d1 26913 d2 d3 d4 d5 int n ; .. .. .. .. }
  • 46. Calculations 26913 / 10 2691 - Division 26913 % 10 3 - Modular division 10 26913 2691 26910 3 Mod
  • 47. The Whole Picture main( ) { … n 26913 d5 3 2691 d4 d3 d2 d1 1 9 6 2 269 26 2 2 d5 = n % 10 ; n = n / 10 ; d4 = n% 10 ; n = n / 10 ; d3 = n % 10 ; n = n / 10 ; d2 = n% 10 ; n = n / 10 ; d1 = n% 10 ; s = d1 + d2 + d3 + d4 + d5 ; printf ( ”%i”, s ) ; } 21
  • 48. Is % ( modulus ) Really Useful  Leap year or not  Odd / Even  Prime or not
  • 49. Where Are We.... Alphabets, Digits, Sp. Symbols Constants, Variables, Keywords Statements or Instructions Program
  • 50. C Statements / Instructions  Type Declaration Instruction Ex. int i, j, k ; float a, b, c ; char ch ;
  • 51. Type Declaration, A few Subtleties int a ; a = 5 ; is same as int a = 5 ;  int a = 5, b = 10, c = a + b * 5% 2 ; Here order is important
  • 52. Is This Ok?  int a, b, c, d ; a = b = c = d = 5 ;   int a = b = c = d = 5 ; 
  • 53. C Statements/Instructions  Type Declaration Instructions  Arithmetic Instructions Ex. s = d1 + d2 + d3 ; si = j * n * r / 100 ; c = 5.0 / 9 * f - 32 ; +, -, *, /, % - Arithmetic Operators
  • 54. Arithmetic Instructions, a few Small Issues  i = j * k + 3 ; j * k + 3 = i ;  i = 3 ; 3 = i ;  a = b ( c + d ) ; a = b * ( c + d ) ;       Tip: Tip:
  • 55. Type of Arithmetic Instructions  Integer mode AI  Real mode AI  Mixed mode AI Ex. int a = 3, b = 4, c ; c = a * b + 5% 6 - b + 14 ; ???? + - % + * =; c a b 5 6 14 Integer mode AI
  • 56. Legal Arithmetic Operations Operand1 Operand2 Result int int float float int float float int int float float float
  • 57. Try This int a ; a = 5 / 2 ; a = 5.0 / 2 ; a = 5 / 2.0 ; a = 5.0 / 2.0 ; a = 2 / 5 ; a = 2.0 / 5 ; a = 2 / 5.0 ; a = 2.0 / 5.0 ; 22 2 2000 0
  • 58. Try This float a ; a = 5 / 2 ; a = 5.0 / 2 ; a = 5 / 2.0 ; a = 5.0 / 2.0 ; a = 2 / 5 ; a = 2.0 / 5 ; a = 2 / 5.0 ; a = 2.0 / 5.0 ; 2.0 2.5 2.5 2.5 0.0 0.4 0.4 0.4
  • 59. Which Is Correct? float c, f = 212.0 ; c = 5 / 9 * ( f - 32 ) ; c = 5 / 9.0 * ( f - 32 ) ; c = ( f - 32 ) * 5 / 9 ; 0.0 100.0 100.0 Note: Parenthesis matter
  • 60. Heirarchy/Priority/Precedence c = a + b * c % 5 - d * 6 1 2 4 Operators * / % + - = 6 3 5
  • 61. C Instructions  Type declarations Instructions  Arithmetic Instructions  Input / Output Instructions printf( ) - Output scanf( ) - Input
  • 62. General Form of printf( ) printf ( ”format string”, list of variables ) ; printf ( ”Enter values of c and d” ) ;  List of variables is optional. printf ( ”%i %i %i”, a, 35, 2 + 6% 3 ) ;  List can contain variables, constants or expressions.
  • 63. printf( ) printf ( ”format string”, list of variables ) ; - int - %i , %d - float - %f - char - %c Escape sequences Any other characters can contain Format specifiers
  • 64. Numbering Systems Binary ( 0 - 1 ) 0 1 10 11 100 ... 111 1000 …. …. Decimal ( 0- 9 ) 0 1 . 9 10 11 .. 99 100 ... ... Octal ( 0- 7 ) 01. 7 10 11 .. 77 100 ... ... Hexadecimal ( 0-9, A- F ) 0 1 . 9 A . F 10 .. FF 100
  • 65. Conversions Deci 473 decimal 4 * 102+ 7 * 101 + 3 * 100 = 473 Octal 11 decimal 1 * 81 + 1 * 80 = 9 Hexa 11 decimal 1 * 161 + 1 * 160 = 17 Binary 11 decimal 1 * 21 + 1 * 20 = 3
  • 66. More Conversions Dec. 9 Octal 11 8 9 8 1 1 0 1 Dec. 17 Hex 11 16 17 16 1 1 0 1 Dec. 3 Binary 11 2 3 2 1 1 0 1
  • 67. printf( ) Makes It Handy  printf ( ”%d %o %x %X”, 10, 10, 10, 10 ) ; 10 12 a A  printf ( ”%d %d %d %d”, 10, 12, a, A ) ; Error  printf ( ”%d %d %d %d”, 10, 012, 0xa, 0XA ) ; 10 10 10 10 zero or O?
  • 68. Escape Sequences  n - Newline  t - Tab  printf ( ”%dn%dt%d”, 10, 20, 30 ) ; 10 20 30
  • 69. Any Other Characters  printf ( ”Enter values of c and d” ) ;  printf ( ”Hello” ) ;  printf ( ”nSimple Interest = Rs. %f”, si ) ; Escape sequence Format Any other specifier character
  • 70. C Instructions Type Declaration Arithmetic Instruction Input/Output Instruction Control Instructions
  • 71. Try At Home Let Us C Chapter 1 - Page 29-36
  • 72. C Instructions  Type Declaration Instructions  Arithmetic Instructions  Input / Output Instructions  Control Instructions
  • 73. Control Instructions  What are they? - Control the sequence of execution of instructions  What different types? - Sequence - Decision - Repitition - Case
  • 74. Normal C Program main( ) { int ..... ; float ...... ; printf ( ...... ) ; scanf ( ...... ) ; a = .... ; b = .... ; printf ( .......) ; } Tip: Sequence CI is the default CI
  • 75. Decision Control Instruction /* Calculation of total expenses */ main( ) { int qty ; float price ; printf ( ”Enter quantity and price” ) ; scanf ( ”%d%f”, & qty, &price ) ; ... ... }
  • 76. /* Calculation of total expenses */ main( ) { int qty ; float price ; printf ( ”Enter quantity and price” ) ; scanf ( ”%d%f”, &qty, &price ) ; } if ( qty >= 1000 ) dis = 10 ; int dis ; float totexp ; 200 15.50 1200 15.50 else dis = 0 ; totexp = qty * price - qty * price * dis / 100 ; printf ( ”Total expenses = Rs %f”, totexp ) ;
  • 77. Tips  if, else - Keywords  General form: if ( condition ) statement1 ; else statement2 ; Relational Operators  Cond. is usually built using <, >, <=, >=, = =, != a = b a = = b Assignment Comparison
  • 78. Would This Work? int a = 3, b = 4, c, d ; printf ( ”%d”, a + b ) ; 7 1 12 0 printf ( ”%d”, a <= b ) ; c = a * b ; printf ( ”%d”, c ) ; d = a = = b ; printf ( ”%d”, d ) ; Tip: Condition - True - Replaced by 1 Condition - False - Replaced by 0
  • 79. Is it Monday or Tuesday int a ; printf ( ”%d”, a ) ; Garbage Tip: Unless specifically initialised a variable contains a garbage value.
  • 80. /* Calculation of Total Expenses */ main( ) { int qty ; float price ; int dis ; printf ( ”Enter quantity and price” ) ; scanf ( ”%d%f” , &qty, &price ) ; if ( qty >= 1000 ) dis = 10 ; totexp = qty * price - qty * price * dis / 100; Tip: else block is optional = 0 float totexp ; printf ( ”Total expenses = Rs %f”, totexp ) ; }
  • 81. /* Calculation of gross salary */ main( ) { float bs, hra, ca, da, gs ; printf ( ”Enter basic salary” ) ; scanf ( ”%f”, &bs ) ; if ( bs >= 1500 ) da = bs * 95 / 100 ; only this hra = bs * 20 / 100 ; belongs to ca = bs * 12 / 100 ; if block else da = bs * 92 / 100 ; only this hra = bs * 15 / 100 ; belongs to ca = 200 ; else block gs = bs + hra + da + ca ; printf ( ”Gross salary = Rs. %f”, gs ) ; }
  • 82. /* Calculation of gross salary */ main( ) { float bs, hra, ca, da, gs ; printf ( ”Enter basic salary” ) ; scanf ( ”%f”, &bs ) ; if ( bs >= 1500 ) { da = … ; hra = ... ; ca = … ; } else { da = … ; hra = ... ; ca = … ; } gs = bs + da + hra + ca ; printf ( ”Gross salary = Rs %f”, gs ) ; }
  • 83. One More Form if ( condition ) { statement1 ; statement2 ; } else { statement3 ; statement4 ; } Tip: - Default scope of if and else is only the next statement after them. - To override the default scope use { }
  • 84. Leap Year or Not main( ) { printf ( ”Enter year” ) ; scanf ( ”%d”, &y ) ; 4 1996 499 1996 0 Leap 4 2000 500 2000 0 Leap 4 1900 475 1900 0 Not Leap int y ; … }
  • 85. main( ) { . . . if ( y % 100 == 0 ) { if ( y % 400 == 0 ) printf ( ”Leap” ) ; else printf ( ”Not Leap” ) ; } else { if ( y % 4 == 0 ) printf ( ”Leap” ) ; else printf ( ”Not Leap” ) ; } } Nested if-else statements are legal.
  • 86. Decide First Day of Any Year main( ) { int y ; printf ( ”Enter year” ) ; scanf ( ”%d”, &y ) ; . . . } 1998
  • 87. The Logic Behind It 8/ 1/ 1 Monday … … … 1/ 1/ 1998 1 /1 / 1 Monday 29/ 1 / 1 ? 1/ 1/ 1 to 28/1 / 1 28 % 7 1/ 1/ 1 to 31/ 12/ 1997 ? % 7 0 ? 1/ 1/ 1 Monday 15/ 1/ 1 Monday 22/ 1/ 1 Monday 29/ 1/ 1 Monday ?
  • 88. main( ) { printf ( ”Enter year” ) ; scanf ( ”%d”, &y ) ; leapdays =( y - 1 ) / 4- ( y - 1 ) / 100 + ( y - 1 ) / 400 ; totaldays = normaldays + leapdays ; firstday = totaldays % 7 ; if ( firstday == 0 ) printf ( ”Monday” ) ; .. .. .. .. } 1/ 1/ 1 to 28/ 1/ 1 28 % 7 1/ 1/ 1 to 31/ 12/ 1997 ? % 7 0 ? int y ;, firstday, normaldays, leapdays, totaldays ; normaldays = ( y - 1 ) * 365 ;
  • 89. main( ) { int y, firstday, leapdays ; long int normaldays, totaldays ; .. .. .. .. normaldays = ( y - 1 ) * 365 ; .. .. .. .. } int -32678 to +32767 long int -2147483648 to +2147783647
  • 90. What’s Wrong Here? normaldays = ( y - 1 ) * 365 ; float a ; a = 5 / 2 ; float a ; a = 5 / 2.0 ; Soln: normaldays = ( y - 1 ) * 365L ;
  • 91. The sizeof( ) Operator 2 4 int i ; long int j ; printf ( ”%d %d”, sizeof ( i ), sizeof ( j ) ) ; printf ( ”%d %d”, sizeof ( int ), sizeof ( long int )); Keyword Operator   2 4
  • 92. Are You Sure? 4 % 3 3 % 4 -3 % 4 3 % -4 -3 % -4 1 3 -3 3 -3 Tip: Sign of remainder is same as sign of numerator
  • 93. Try Your Hand At Exploring C Chapter 1 Let Us C Chapter 2 Pg 60 - 64 ( e )
  • 94. What Will Be The Output main( ) { int a = 5, b = 10 ; if ( a >= 20 ) ; b = 30 ; 30 printf ( ”%d”, b ) ; } ; - Null statement. Doesn’t do anything on execution
  • 95. What Will Be The Output main( ) { int a = 10 ; if ( a = 5 ) printf ( ”Hi” ) ; } if ( a = 5 ) if ( a ) if ( 5 ) Tip : Truth in C is nonzero, whereas, falsity is zero.
  • 96. main( ) { if ( per >= 60 ) int per ; printf ( ”First division” ) ; else { if ( per >= 50 ) printf ( ”Second division” ) ; Three Problems int m1, m2, m3, m4, m5 ; printf ( ”Enter marks in five subjects” ) ; scanf ( ”n%d%d%d%d%d”, &m1, &m2, &m3, &m4, &m5 ); per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ; else { if ( per >= 40 ) printf ( ”Third division” ) ; else printf ( ”Fail” ) ; } } }
  • 97. main( ) { int m1, m2, m3, m4, m5, per ; printf ( ”Enter marks in five subjects” ) ; scanf ( ”%d%d%d%d%d”, &m1, &m2, &m3, &m4, &m5 ) ; per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ; if ( per >= 60 ) printf ( ”First division” ) ; if ( per >= 50 ) printf ( ”Second division” ) ; if ( per >= 40 ) printf ( ”Third division” ) ; else printf ( ”Fail” ) ; } Anything Wrong? ?
  • 98. main( ) { int m1, m2, m3, m4, m5, per ; printf ( ”Enter marks in five subjects” ) ; scanf ( ”%d%d%d%d%d”, &m1, &m2, &m3, &m4, &m5 ) ; per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ; if ( per >= 60 ) printf ( ”First division” ) ; if ( per >= 50 && per < 60 ) printf ( ”Second division” ) ; if ( per >= 40 && per < 50 ) printf ( ”Third division” ) ; else printf ( ”Fail” ) ; } Still Anything Wrong?
  • 99. main( ) { int m1, m2, m3, m4, m5, per ; printf ( ”Enter marks in five subjects” ) ; scanf ( ”%d%d%d%d%d”, &m1, &m2, &m3, &m4, &m5 ) ; per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ; if ( per >= 60 ) printf ( ”First division” ) ; if ( per >= 50 && per < 60 ) printf ( ”Second division” ) ; if ( per >= 40 && per < 50 ) printf ( ”Third division” ) ; if ( per < 40 ) printf ( ”Fail” ) ; } Is This Better?
  • 100. Logical Operators && - And || - Or ! - Not Useful in Checking Ranges Yes / No Problem
  • 101. main( ) { int age ; char s, ms ; printf ( ”Enter age, sex, marital status” ) ; scanf ( ”%d%c%c”, &age, &s, &ms ) ; if ( ms == m ) 26 m u ’ ’ printf ( ”Insured” ) ; else { if ( s == m ) { if ( age > 30 ) printf ( ”Insured” ) ; else printf ( ”Not Insured” ) ; } else { if ( age > 25 ) printf ( ”Insured” ) ; else printf ( ”Not In.” ) ; } } } ’ ’ Not Insured
  • 102. Program Using Logical Operators main( ) { int age ; char s, ms ; printf ( ”Enter age, sex, marital status” ) ; scanf ( ”%d%c%c”, &age, &s, &ms ) ; } 26 m u if ( ms = = ’m’ ms = = ’u’ s = = ’m’ age > 30 ( ) || ( ) ( && && ) ms = = ’u’ s = = ’f’ age > 25 ) || printf ( ”Insured” ) ; else printf ( ”Not Insured” ) ; && && Not Insured
  • 103. Working of && And || cond1 cond2 cond1 && cond2 cond1 || cond2 True True True True False False True False False True False False False True False True
  • 104. Hierarchy of Operators Operators Type ! Logical Not * / % Arithmetic + - Arithmetic < > <= >= Relational = = != Relational && Logical And || Logical Or = Assignment Increasing Priority Other Unary Operators - + & sizeof
  • 105. Exchanging Blocks main( ) { int a = 3, b = 4; if ( a <= b ) printf ( ”A”) ; else printf ( ”B” ) ; } main( ) { int a = 3, b = 4 ; } a > b if ( ) printf ( ”B” ) ; else printf ( ”A” ) ; Output: A Output: A
  • 106. One More Way main( ) { int a = 3, b = 4 ; if ( a <= b ) printf ( ”A” ) ; else printf ( ”B” ) ; } main( ) { int a = 3, b = 4 ; if ( ) printf ( ”A” ) ; else printf ( ”B” ) ; } ! a > b Output: A Output: B
  • 107. The Correct Way main( ) { int a = 3, b = 4 ; if ( ! ( a > b ) ) printf ( ”A” ) ; else printf ( ”B” ) ; } Output: A
  • 108. Yet Another Way main( ) { int a = 3, b = 4 ; if ( a > b ) printf ( ”B” ) ; else printf ( ”A” ) ; } Output: A main( ) { int a = 3, b = 4 ; if ( ) printf ( ”B” ) ; else printf ( ”A” ) ; } ! ( a <= b ) Output: A
  • 109. What Would be The Output main( ) { int a = 3, b = - 4, c ,d ; c = !a ; 0 d = !b ; printf ( ”%d %d %d %d”, c, d, a, b ) ; } 0 3 - 4
  • 110. Point Out The Error main( ) { int a = 3, b = 4, c ; Lvalue Required c = !a || b = 7 ; printf ( ”%d %d”, b, c ) ; }
  • 111. What Would be The Output main( ) { int a = 3, b = 4, c ; c = !a || ( b = 7 ) ; printf ( ”%d %d”, b, c ) ; } 7 1
  • 112. Yet Another Way main( ) { ? : Conditional operators How are they different? main( ) { int a = 3, b = 4 ; if ( a <= b ) printf ( ”A” ) ; else printf ( ”B” ) ; } Output: A a <= b printf ( ”A” ) printf ( ”B” ) ; } int a = 3, b = 4 ;
  • 113. How Would You Convert main( ) { int a = 3, b = 4 ; if ( a <= b ) printf ( ”A” ) ; } main( ) { int a = 3, b = 4 ; a <= b ? printf ( ”A” ) ; } Error
  • 114. Remove The Error main( ) { int a = 3, b = 4 ; a <= b ? printf ( ”A” ) : ; } Necessary
  • 115. No Errors At Last main( ) { Null Statement int a = 3, b = 4 ; a <= b ? printf ( ”A” ) : ; ; } TTeerrmmiinnaattoorr Dummies Soln: a <= b ? printf ( ”A” ) : printf ( ” ” ) ; a <= b ? printf ( ”A” ) : ( z = 3 ) ;
  • 116. Moral of The Story Use one, use another Dummy statement Not a replacement for if-else Only 1 statement in ? part and one in : part
  • 117. Some More Different Ways main( ) { int a = 3, b ; a <= 5 ? ( b = 10 ) : ( b = 20 ) ; printf ( ”%d”, b ) ; } b = a <= 5 ? 10 : 20 ; printf ( ”%d”, b ) ; ( ) Necessary  printf ( ”%d”, a <= 5 ? 10 : 20 ) ;
  • 118. Practice Hasn’t Harmed Anybody Exploring C Chapter 2 Let Us C Chapter 2
  • 119. Conversions main( ) { 10 20 3.140000 6.280000 int a = 10, b = 20 ; float c = 3.14 , d = 6.28 ; printf ( ”%d %d %f %f ”, a, b, c, d ) ; printf ( ”%d %d %f %d ”, a, b, c, d ) ; printf ( ”%f %d %f %f ”, a, b, c, d ) ; } 10 20 3.140000 0 2.68 e 154 7864 0.000000 -1.827 e 259
  • 120. Conversions Continued... main( ) { int i = 65, j = 90 ; char ch = ’A’, dh = ’Z’ ; 65 90 A Z printf ( ”%d %d %c %c”, i, j, ch, dh ) ; A Z printf ( ”%c %c”, i, j ) ; printf ( ”%d %d”, ch, dh ) ; } 65 90
  • 121. int i = 65 ; float a = 3.14 char ch = ’A’ i a ch 2 65 2 32 1 2 16 0 2 8 0 2 4 0 2 2 0 2 1 0 0 1 ’A’ Divisions, multiplications illegal Some method involving division by 2 and multiplication by powers of 2 Dec 65 Bin 01000001 i a ? 3.14 ch 01000001 0’s and 1’s ?
  • 122. Our Own Scheme  2 Binary Digits 00 01 10 11 A B C D  3 Binary Digits 000 001 010 011 100 101 110 111 A B C D E F G H  4 Binary Digits 24 = 16 combinations  5 Binary Digits 25 = 32 combinations 00000 00001 00010 .. .. .. .. 26 Combinations A B C .. .. .. .. 26 Alphabets
  • 123. Characters to be Represented  26 Capital letters ( A- Z )  26 Smallcase letters ( a - z )  10 Digits ( 0 - 9 )  32 Special Symbols ( + - * / . : ; etc. )  34 Non-printable chars - control chars  128 Graphic characters 256 Characters
  • 124. Our Own Scheme 2 Bits 22 = 4 Combinations 3 Bits 23 = 8 Combinations 4 Bits 24 = 16 Combinations 5 Bits 25 = 32 Combinations 6 Bits 26 = 64 Combinations 7 Bits 27 = 128 Combinations 8  Bits 28 = 256 Combinations
  • 125. Graphics Characters Character ASCII value 218 191 192 217 196 179
  • 126. Workable Scheme? 00001111 01001111 10101010 256 combinations A B C 256 characters 01000001 A 01000010 B 01000011 C 01000100 D 01000101 E …. .. …. .. ASCII Codes
  • 127. Binary Is Difficult Binary A 01000001 B 01000010 C 01000011 D 01000100 E 01000101 .. …. Decimal 65 66 67 68 69 .. Character
  • 128. Characters to be Represented  26 Capital letters ( 65 - 90 )  26 Smallcase letters ( 97 - 122 )  10 Digits ( 48 - 57 )  32 Special Symbols ( )  34 Non-printable chars ( 0 - 33 )  128 Graphic characters ( 128 - 255 ) 256 Characters ( 0 - 255 )
  • 129. Methods Are Different int i = 65 ; char ch = ’A’ ; i ch 01000001 01000001 printf ( ”%c”, i ) ; printf ( ”%d”, ch ) ; A 65
  • 130. What About More Than 255 int i = 300, j = 65; printf ( ”%c”, i ) ; printf ( ”%c”, j ) ; i Only lower byte gets used 00000001 00101100 Higher Lower j 00000000 01000001
  • 131. Why Unreliable Conversions float i = 3.14 ; printf ( ”%c”, i ) ; printf ( ”%d”, i ) ; i 0s and 1s 0s and 1s 0s and 1s 0s and 1s int i = 35 ; printf ( ”%f”, i ) ; i 0s and 1s 0s and 1s
  • 132. Control Instructions  Sequence  Decision - if-else - ? : - < > <= >= = = != - && || !  Repitition / Loop control instruction
  • 133. To Begin With... main( ) { int p, n ; float r, si ; printf ( ”Enter values of p, n, r” ) ; scanf ( ”%d%d%f”, &p, &n, &r ) ; si = p * n * r / 100 ; printf ( ”Simple interest = Rs %f”, si ) ; }
  • 134. Repeat with while main( ) { int p, n ; float r, si ; while printf ( ”Enter values of p, n, r” ) ; scanf ( ”%d%d%f”, &p, &n, &r ) ; si = p * n * r / 100 ; printf ( ”Simple interest = Rs %f”, si ) ; } Tip : while - Keyword
  • 135. Any Condition main( ) { int p, n, i ; float r, si ; while ( ) printf ( ”Enter values of p, n, r” ) ; scanf ( ”%d%d%f”, &p, &n, &r ) ; si = p * n * r / 100 ; printf ( ”Simple interest = Rs %f”, si ) ; } i <= 10
  • 136. Initialization Necessary main( ) { int p, n, i ; float r, si ; while ( i <= 10 ) printf ( ”Enter values of p, n, r” ) ; scanf ( ”%d%d%f”, &p, &n, &r ) ; si = p * n * r / 100 ; printf ( ”Simple interest = Rs %f”, si ) ; } i = 1 ;
  • 137. So Also Incrementation main( ) { int p, n, i ; float r, si ; i = 1 ; while ( i <= 10 ) printf ( ”Enter values of p, n, r” ) ; scanf ( ”%d%d%f”, &p, &n, &r ) ; si = p * n * r / 100 ; printf ( ”Simple interest = Rs %f”, si ) ; } i = i + 1 ;
  • 138. Scope And Default Scope main( ) { int p, n, i = 1 ; float r, si ; while ( i <= 10 ) { printf ( ”Enter values of p, n, r” ) ; scanf ( ”%d%d%f”, &p, &n, &r ) ; si = p * n * r / 100 ; printf ( ”Simple interest = Rs %f”, si ) ; i = i + 1 ; } }
  • 139. Logic Doesn’t Matter main( ) { int i ; i = 1 ; while ( i <= 10 ) { /* any logic */ i = i + 1 ; } } Loop Counter / Index variable /* Initialisation of loop counter */ /* Testing of loop counter */ /* Incrementation of loop counter*/
  • 140. More On Loop Counters  Loop counters can be integers, long integers, floats or characters  Loop counters can be incremented or decremented by any suitable step value
  • 141. Another Program /* Print numbers from 1 to 10 */ main( ) { int i = 1 ; printf ( ”%d”, i ) ; }
  • 142. Put it in a Loop /* Print numbers from 1 to 10 */ main( ) { int i = 1 ; while ( i <= 10 ) { printf ( ”%d”, i ) ; } } i = i + 1 ;
  • 143. Another Way /* Print numbers from 1 to 10 */ main( ) { int i = 1 ; while ( i <= 10 ) { printf ( ”%d”, i ) ; } } i ++ ;
  • 144. Incrementation / Decrementation Operators ++ Increases value of a variable by 1  -- Decreases value of a variable by 1  ** // %% Don’t make sense
  • 145. Are They OK  i = i + 1 i++  i = i + 2 i+++  i = i + 10 ? Tip : +++ doesn’t exist
  • 146. Would This Work     i = j---2 ;  int i, j = 3 ; i = --j ; i = --3 ;  i = j----2 ;  i = j----2 ;  
  • 147. Space Matters   i = j----2 ; i = j-- - -2 ; Optional Complusory
  • 148. Is i++ Same As ++i main( ) { int i = 1 ; while ( i <= 10 ) 1 st oper. - Printing 2 nd oper. - Incrementation printf ( ”%d”, i++ ) ; } Output : 1 2 3 4 ……9 10
  • 149. main( ) { int i = 1 ; while ( i <= 10 ) 1 st oper. - Incrementation 2 nd oper. - Printing printf ( ”%d”, ++i ) ; } Output : Using ++i 2 3 4 5 ……9 10 11
  • 150. The Correct Way main( ) { int i = 0 ; while ( i < 10 ) printf ( ”%d”, ++i ) ; } Output : 1 2 3 4 ……9 10
  • 151. Moral? main( ) { int i = 1 ; while ( i <= 10 ) printf ( ”%d”, i++ ) ; } main( ) { int i = 0 ; while ( i < 10 ) printf ( ”%d”,++ i ) ; }
  • 152. Two More Ways main( ) { int i = 0 ; while ( i++ < 10 ) printf ( ”%d”, i ) ; } main( ) { int i = 0 ; while ( ++i <= 10 ) printf ( ”%d”, i ) ; } 1 st oper. - Incr 2 nd oper. - Testing 1 st oper. - Testing 2 nd oper. - Incr
  • 153. Compare main( ) { int i = 1 ; while ( i <= 10 ) printf ( ”%d”, i++ ) ; } main( ) { int i = 0 ; while ( i < 10 ) printf ( ”%d”,++ i ) ; } main( ) { int i = 0 ; while ( ++i <= 10 ) printf ( ”%d”, i ) ; } main( ) { int i = 0 ; while ( i++ < 10 ) printf ( ”%d”, i ) ; }
  • 154. While ( We Don’t Meet Again ) Let Us C Page 96-99 ( f )
  • 155. Print Nos. From 1 to 10 main( ) { int i = 1 ; while ( i <= 10 ) { printf ( ”%d”, i ) ; } } a = a * 10 ; a *= 10 ; b = b % 5 ; b %= 5 ; i += 1 ; Same as i++, ++i, i = i + 1 += -= *= /= %= Compound Assignment Oper.
  • 156. One More Way main( ) { int i = 1 ; while ( ) { Any Non-Zero number printf ( ”%d”, i ) ; i++ ; } } 1 /* Repeat infinite times */
  • 157. Applying Brakes main( ) { break - Terminates Loop exit( ) - Terminates Program int i = 1 ; while ( 1 ) /* Repeat infinite times */ { printf ( ”%d”, i ) ; i++ ; } } if ( i > 10 ) break ; if ( i > 10 ) exit( ) ; Keyword
  • 158. Calculate n! n i i = 1 xn 1 * 2 * 3 * 4 *….n 1 + 2 + 3 + 4 +.…n 2 * 2 * 2 * 2 *….n Running product Running sum Running product
  • 159. Running Sum And Products int s, p, pr, i, n ; i = 1 ; while ( i <= n ) { s = 0 ; p = 1 ; pr = 1 ; s = s + ; } i p = p * i ; pr = pr * 2 ; 1 + 2 + 3 + 4 +.…n RS 1 * 2 * 3 * 4 *….n RP 2 * 2 * 2 * 2 *….n RP main( ) { scanf ( ”%d”, &n ) ; i++ ; printf ( ”%d %d %d”, s, p, pr ) ; } i s 1 0 2 1 3 3 4 6 5 10 6 15 5
  • 160. x x3 x5 x7 x9 — - — + — - — + — - …………10 terms 1! 3! 5! 7! 9! main( ) { scanf ( ”%f”, &x ) ; while ( i <= 10 ) { i = 1 ; s = 0 ; calculate numerator calculate denominator term = numerator / denominator Add / subtract alternate terms to/from s i++ ; } print ( s ) ; }
  • 161. main( ) { scanf ( ”%f ”, &x ) ; i = 1 ; while ( i <= 10 ) { OK ? float x, s, n, d, t ; int i, j, k, n ; k = n = d = 1 ; i++ ; } printf ( ”%f ”, s ) ; } x1 x3 x5 —1! - — 3! + — 5! - i j 1 1 2 3 3 5 4 7 5 9 .. .. j = 2 * i - 1 ; while ( k <= j ) { d = d * k ; n = n * x ; k++ ; } t = n / d ; ( i % 2 == 0 ) ? s = s - t : s = s + t ; Anything wrong ? s = 0 ; Associates from R to L
  • 162. Prime Number or Not main( ) { printf ( ”Enter any number” ) ; scanf ( ”%d”, &n ) ; ... ... } 13 2 3 4 .. .. .. 12 int n ; 13
  • 163. Prime No. main( ) { int n ; printf ( ”Enter any number” ) ; scanf ( ”%d”, &n ) ; } 13 2 3 4 .. .. .. 12 int i = 2 ; while ( ) i <= n - 1 { if ( n % i = = 0 ) printf ( ”Not a prime number” ) ; else i++ ; }
  • 164. main( ) { int n, i = 2 ; printf ( ”Enter any number” ) ; scanf ( ”%d”, &n ) ; while ( i <= n - 1 ) 13 { if ( n % i == 0 ) printf ( ”Not a prime number” ) ; break ; { } else i++ ; } } if ( i == n ) printf ( ”Prime number” ) ;
  • 165. Generate Prime Numbers 2 17 3 19 5 23 7 . 11 . 13 199
  • 166. i = ++a && ++b && ++c ; j = ++a || ++b || ++c ; What Would Be The O/P b = ( ++i && ++j ) || ++k ; main( ) { int i = 5, j = 4, k = -1, a, b ; a = i && !j ; b = ++i && ++j || ++k ; printf ( ”%d %d %d %d %d”, a, b, i, j, k ) ; } b = ++i && ( ++j || ++k ); 0 1 6 5 -1
  • 167. Loop Control Instructions while for  do-while
  • 168. The while Loop main( ) { int m1, m2, m3, avg, i ; i = 1 ; while ( i <= 10 ) { scanf ( ”%d%d%d”, &m1, &m2, &m3 ) ; avg = ( m1 +m2 + m3 ) / 3 ; printf ( ”%d”, avg ) ; i++ ; } } Init Test Incr Test Test Incr Incr Test Incr .. .. .. ..
  • 169. The for Loop main( ) { int m1, m2, m3, avg, i ; scanf ( ”%d%d%d”, &m1, &m2, &m3 ) ; avg = ( m1 + m2 + m3 ) / 3 ; printf ( ”%d”, avg ) ; } ( ) Init Test Incr Test Test Incr Incr Test Incr .. .. for .. .. i = 1 ; i <= 10 ; i++ { }
  • 170. Comparision - I Infinite Loop Finite Loop main( ) { int i = 1 ; while ( i <= 10 ) statement1 ; statement2 ; statement3 ; i++ ; } main( ) { int i ; for ( i = 1 ; i <= 10 ; i++ ) statement1 ; statement2 ; statement3 ; }
  • 171. Comparison II Infinite Loop Finite Loop main( ) { int i = 1 ; while ( i <= 10 ) ; { statement1 ; statement2 ; statement3 ; i++ ; } } main( ) { int i ; for ( i = 1 ; i <= 10 ; i++ ) ; { statement1 ; statement2 ; statement3 ; } }
  • 172. Print Nos. From 1 to 10 main( ) { int i ; for ( i = 1 ; i <= 10 ; i++ ) printf ( ”n%d”, i ) ; }
  • 173. Dropping Initialisation main( ) { int i = 1 ; for ( ; i <= 10 ; i++ ) printf ( ”n%d”, i ) ; }
  • 174. Dropping Incrementation main( ) { int i = 1 ; for ( ; i <= 10 ; ) { printf ( ”n%d”, i ) ; i++ ; } }
  • 175. Dropping Condition main( ) ; ; Necessary { int i = 1 ; for ( ; ; ) { printf ( ”%d”, i ) ; i++ ; } } if ( i > 10 ) break ; printf ( ”%d”, i++ ) ; if ( ++i > 10 )
  • 176. Comparison III Error Infinite Loop main( ) { int i = 1 ; while ( ) { statement1 ; statement2 ; statement3 ; } } main( ) { int i = 1 ; for ( ; ; ) { statement1 ; statement2 ; statement3 ; } }
  • 177. Drawing Boxes 012..... 24 0 1 2 … … … … 79 (10,20) Tip : ( 10, 20 ) (23,70) ( row number, col number )
  • 178. Box Drawing Characters Character ASCII value 218 191 192 217 196 179
  • 179. To Begin With... main( ) { clrscr( ) ; printf ( ”%c”, 218 ) ; … … }
  • 180. main( ) { clrscr( ) ; } {} int r, c ; (10,20) gotorc ( 10, 20 ) ; printf ( ”%c”, 218 ) ; gotorc ( 10, 70 ) ; printf ( ”%c”, 191 ) ; gotorc ( 23, 20 ) ; printf ( ”%c”, 192 ) ; gotorc ( 23, 70 ) ; printf ( ”%c”, 217 ) ; for ( r = 11 ; r < 23 ; r++ ) { gotorc ( r, 20 ) ; printf ( ”%c”, 179 ) ; gotorc ( r, 70 ) ; printf ( ”%c”, 179 ) ; } for ( … … … .. ) (23,70)
  • 181. #include ”goto.c” main( ) { int r, c ; clrscr( ) ; gotorc ( 10, 20 ) ; printf ( ”%c”, 218 ) ; .. .. .. .. for ( r = 11 ; r < 23 ; r++ ) { .. .. } for ( … … … ) {} }
  • 182. 96 - 99 Chapter 3
  • 183. 1 1 1 1 1 2 1 1 3 1 2 1 1 2 2 1 2 3 1 3 1 1 3 2 1 3 3 3 1 1 3 1 2 3 1 3 3 2 1 3 2 2 3 2 3 3 3 1 3 3 2 3 3 3 Combinations 2 1 1 2 1 2 2 1 3 2 2 1 2 2 2 2 2 3 2 3 1 2 3 2 2 3 3
  • 184. Starting Off... i = 1 ; j = 1 ; for ( k = 1 ; k <= 3 ; k++ ) { printf ( ”n%d%d%d”, i, j, k ) ; } } 1 1 1 1 1 2 1 1 3 1 2 1 1 2 2 1 2 3 1 3 1 1 3 2 1 3 3 main( ) { int i, j, k ; i j k
  • 185. Adding One More Loop main( ) { int i, j, k ; i = 1 ; for ( k = 1 ; k <= 3 ; k++ ) printf ( ”n%d%d%d”, i, j, k ) ; } for ( j = 1 ; j <= 3 ; j++ ) { i j k 1 1 1 1 1 2 1 1 3 1 2 1 1 2 2 1 2 3 1 3 1 1 3 2 1 3 3 }
  • 186. main( ) { int i, j, k; for ( j = 1 ; j <= 3 ; j ++ ) { for ( k = 1 ; k <= 3 ; k++ ) printf ( ”n%d%d%d”, i, j, k ) ; } } for ( i = 1 ; i <= 3 ; i ++ ) { 1 1 1 .. .. .. .. .. .. 1 3 3 2 1 1 .. .. .. .. .. .. 2 3 3 3 1 1 .. .. .. .. .. .. 3 3 3 } Finishing Off...
  • 187. Unique Combinations main( ) { int i, j, k ; for ( i = 1 ; i <= 3 ; i++ ) { for ( j = 1 ; j <= 3 ; j++ ) { for ( k = 1 ; k <= 3 ; k++ ) { printf ( ”%d %d %d”, i, j, k ) ; } } } } .. .. .. 1 2 3 .. .. .. 1 3 2 .. .. .. 2 1 3 .. .. .. 2 3 1 .. .. .. 3 1 2 .. .. .. 3 2 1 if ( i != j && j != k && k != i )  if ( i != j != k ) printf ( ”%d %d %d”, i, j, k ) ;
  • 188. One More Way main( ) { int i, j, k ; for ( i = 1 ; i <= 3 ; i++ ) { for ( j = 1 ; j <= 3 ; j++ ) { for ( k = 1 ; k <= 3 ; k++ ) { printf ( ”%d %d %d”, i, j, k ) ; } } } } i j k 1 1 1 2 1 3 1 if ( i = = j || j = = k || k = = i ) break ; else
  • 189. The Correct Way main( ) { int i, j, k ; for ( i = 1 ; i <= 3 ; i++ ) { for ( j = 1 ; j <= 3 ; j++ ) { for ( k = 1 ; k <= 3 ; k++ ) { if ( i == j || j == k || k == i ) continue ; else printf ( ”%d %d %d”, i, j, k ) ; } } } } i j k 1 1 1 2 3 1 2 3 2 4 Would ; be OK?
  • 190. Multiple Initializations main( ) { int i, j, k ; i = 1 ; j = 3 ; for ( k = 1 ; k <= 5 ; k++ ) { …… i += 2 ; j *= 4 ; } }
  • 191. Better Way for ( i = 1, j = 3, k = 1 ; k <= 5 ; i += 2, j *= 4 ) { …… } Multiple Conditions? Can I Drop k++ Connect using && and ||
  • 192. Type of Loops  while  for  Which is  do - while better
  • 193. The do-while Loop main( ) { int i = 1 ; do { printf ( ”n%d”, i ) ; i++ ; } while ( i <= 10 ) ; } Tip1: { } - Necessary Tip2: ; after while - Necessary
  • 194. Compare do { printf ( ”Hi” ) ; } while ( 4 < 1 ) ; for ( ; 4 < 1 ; ) printf ( ”Hi” ) ; while ( 4 < 1 ) printf ( ”Hi” ) ; Hi No Output No Output
  • 195. Which To Use When  for - Finite no. of times  while - Unknown no. of times  do - while - At least once Recommended, Not compulsory
  • 196. Unknown No. of Times Foolproof? while ( ) { } } char ch = ’y’ ; ch == ’y’ main( ) { /* some logic */ printf ( ”Continue Y/N” ) ; scanf ( ”%c”, &ch ) ; Solution while ( ch == ’y’ || ch == ’Y’ ) while ( toupper ( ch ) == ’Y’ ) while ( tolower ( ch ) == ’y’ )
  • 197. Control Instructiopns  Sequence  Decision  Repetition  - while, for, do-while break, continue - ++, -- - +=, -=, *=, /=, %=  Case
  • 198. main ( ) { int n ; printf ( ”Enter no. between 1 and 3” ) ; scanf ( ”%d”, &n ) ; if ( n = = 1 ) printf ( ”You entered 1” ) ; else { if ( n = = 2 ) printf ( ”You entered 2” ) ; else { if ( n = = 3 ) printf ( ”You entered 3” ) ; else printf ( ”Wrong choice” ) ; } } } Three Problems
  • 199. Alternative Use Logical Operators   Use case Control Instruction
  • 200. main( ) { int n ; printf ( ”Enter no. below 1 and 3” ) ; scanf ( ”%d”, &n ) ; { case 1 : printf ( ”You Entered 1” ) ; case 2 : printf ( ”You Entered 2” ) ; case 3 : printf ( ”You Entered 3” ) ; else printf ( ”Wrong Choice” ) ; } } switch ( n ) 
  • 201. main( ) { Tip: If a case fails control jumps to the next case int n ; printf ( ”Enter no. between 1 and 3” ) ; scanf ( ”%d”, &n ) ; { case 1 : 2 Output: You entered 2 printf ( ”You entered 1” ) ; case 2 : printf ( ”You entered 2” ) ; case 3 : printf ( ”You entered 3” ) ; } } switch ( n ) default : printf ( ”Wrong Choice” ) ;
  • 202. main ( ) { int n ; scanf ( ”%d”, &n ) ; switch ( n ) { case 1 : printf ( ”You entered 1” ) ; case 2 : printf ( ”You entered 2” ) ; case 3 : printf ( ”You entered 3” ) ; default : printf ( ”Wrong choice” ) ; } } Tip: If a case is satisfied all statements below it are executed Output: You entered 2 You entered 3 2 Wrong choice
  • 203. main ( ) { int n ; scanf ( ”%d”, &n ) ; switch ( n ) { case 1 : printf ( ”You Entered 1” ) ; case 2 : printf ( ”You Entered 2” ) ; case 3 : printf ( ”You Entered 3” ) ; default : printf ( ”Wrong choice” ) ; } } The Solution break ; break ; break ; 2
  • 204. main ( ) { int n ; scanf ( ”%d”, &n ) ; switch ( n ) { case 1 : printf ( ”You Entered 1” ) ; break ; case 2 : printf ( ”You Entered 2” ) ; continue ; case 3 : printf ( ”You Entered 3” ) ; break ; default : printf ( ”Wrong choice” ) ; } } Illegal What If continue
  • 205. main ( ) { int n ; scanf ( ”%d”, &n ) ; switch ( n ) { case 2 : printf ( ”You Entered 2” ) ; break ; case 1 : printf ( ”You Entered 1” ) ; break ; case 3 : printf ( ”You Entered 3” ) ; break; default : printf ( ”Wrong choice” ) ; } } Tip: Order of cases is unimportant
  • 206. main ( ) { int n ; scanf ( ”%d”, &n ) ; switch ( n ) { default : printf ( ”Wrong choice” ) ; case 2 : break ; printf ( ”You Entered 2” ) ; break ; case 1 : printf ( ”You Entered 1” ) ; break ; case 3 : printf ( ”You Entered 3” ) ; break ; } } Tip: Even default can be the very first case
  • 207. main ( ) { int n ; scanf ( ”%d”, &n ) ; switch ( n ) { case 2 : printf ( ”You Entered 2” ) ; break ; case 1 : printf ( ”You Entered 1” ) ; break ; case 3 : printf ( ”You Entered 1” ) ; } } Tip: default case is optional
  • 208. main( ) { char ch ; printf ( ”Enter alphabet between A and C” ) ; scanf ( ”%c”, &ch ) ; switch ( ch ) { case ’A’: printf ( ”You entered A” ) ; break ; case ’B’: printf ( ”You entered B” ) ; break ; case ’C’: printf ( ”You entered C” ) ; break ; } }
  • 209. main( ) { char ch ; printf ( ”Enter alphabet between A and C” ) ; scanf ( ”%c”, &ch ) ; switch ( ch ) { case ’A’ || ’a’ : printf ( ”You entered A” ) ; break ; case ’B’ || ’b’ : printf ( ”You entered B” ) ; break ; case ’C’ || ’c’ : printf ( ”You entered C” ) ; break ; } } Become case 1 :
  • 210. main( ) { char ch ; printf ( ”Enter alphabet between A and C” ) ; scanf ( ”%c”, &ch ) ; switch ( ch ) { case ’A’: printf ( ”You entered A” ) ; break ; case ’B’: printf ( ”You entered B” ) ; break ; case ’C’: printf ( ”You entered C” ) ; break ; } } case ’a’ : A case ’b’ : case ’c’ : a
  • 211. main( ) { int n = 2 ; int a = 1, b = 2 ; switch ( n ) { case a : … case b : … case 3 : … } } Would This Work 
  • 212. General Form main( ) { n + 3 / a + 2 4 + 3 / 5 + 2 switch ( expression ) { case constant expression : … case constant expression : … case constant expression : … default: … } } 3 + 2 % 5
  • 213. Checking switch  int  char  long int  float    ? Tip: float cannot be checked using switch
  • 214. Is switch a replacement for if ?
  • 215. 99 [D] - 102 Chapter 3
  • 216. Menu Management 1. Odd / Even 2. Leap Year 3. Prime No. 0. Exit Your Choice? 20 10
  • 217. # include ”goto.c” main( ) { clrscr( ) ; gotorc ( 10, 20 ) ; printf ( ”1. Odd / Even” ) ; gotorc ( 11, 20 ) ; printf ( ”2. Leap year” ) ; gotorc ( 12, 20 ) ; printf ( ”3. Prime number” ) ; gotorc ( 13, 20 ) ; printf ( ”0. Exit” ) ; gotorc ( 15, 20 ) ; printf ( ”Your choice?” ) ; scanf ( ”%d”, &choice ) ; .. .. } int choice ;
  • 218. main( ) { int choice ; /* display menu */ scanf ( ”%d”, &choice ) ; switch ( choice ) { case 1 : /* odd / even logic */ break ; case 2 : /* leap year logic*/ break ; case 3 : /*prime number logic*/ break ; default : printf ( ”a” ) ; } } case 0 : break ; n t a Escape Sequences printf ( ”aaa” ) ; Long Beep
  • 219. switch ( choice ) { case 1 : break ; case 2 : break ; case 3 : break ; case 0 : break ; default : printf ( ”a” ) ; } Block Commands CH2PR3.C Odd / even logic CH2PR4.C Leap year logic CH2PR5.C Prime no. logic
  • 220. Other Block Commands Ctrl KB Ctrl KK Mark block Ctrl KV Move block Ctrl KC Copy block Ctrl KW Write block to file Ctrl KY Delete block Ctrl KH Hide or unmark block
  • 221. Readjustments Necessary  case 1: main( ) { Delete Delete …… } break ;  Shift declarations Only Shifting Won’t Do
  • 222. No while, No Menu #include ”goto.c” main( ) { while ( 1 ) { /*display menu*/ scanf ( ”%d”, &choice ) ; switch( choice ) { case 1 : … break ; case 0 : break ; } } int choice ; } exit( ) ;
  • 223. Requirements of A Menu  Infinite loop  switch
  • 224. Surprised? main( ) { float a = 0.7 ; if ( a < 0.7 ) printf ( ”A” ) ; else printf ( ”B” ) ; } A
  • 225. Appearances Are Misleading main( ) { float a = 0.7 ; printf ( ”%d%d”, sizeof ( a ), sizeof ( 0.7 ) ); } 365 0.7 Integer Double 4 8
  • 226. Binary of A Float float a = 5.375 ; Binary of 5 Binary of .375 101 .011 Binary of 5.375 101.011 Normalised form 1.01011 * 22 .375 * 2 = 0.750 0 .750 * 2 = 1.500 1 .500 * 2 = 1.000 1
  • 227. The Actual Storage Mantissa 32 0 1000 0001 01011000000000000000000 Positive 2 + 127 . 01011 1 8 23 Sign Bit Biased Exponent
  • 228. Difference In double Mantissa 64 1 11 52 Bias value is 1023 Sign Bit Biased Exponent
  • 229. Binary of 0.7 .7 * 2 = 1.4 1 .4 * 2 = 0.8 0 .8 * 2 = 1.6 1 .6 * 2 = 1.2 1 .2 * 2 = 0.4 0 .4 * 2 = 0.8 0 .8 * 2 = 1.6 1 .6 * 2 = 1.2 1 .2 * 2 = 0.4 0 .4 * 2 = 0.8 0 . . . . . . . . . . . .
  • 230. 32 - bit Recurring binary equivalent of 0.7 64 - bit Recurring binary equivalent of 0.7 < float a = 0.7 ; if ( a < 0.7 ) printf ( ”A” ) ; else printf ( ”B” ) ; A
  • 231. Overriding Defaults 365 365L 0.7 0.7f if ( a < 0.7f ) printf ( ”A” ) ; else printf ( ”B” ) ; B
  • 232. Exact Binary main( ) { float a = 5.375 ; if ( a < 5.375 ) printf ( ”A” ) ; else printf ( ”B” ) ; } B
  • 233. Functions main( ) { printf ( ”n I am in main” ) ; } bombay( ) { printf ( ”n I am in Bombay” ) ; } kanpur( ) { printf ( ”n I am in Kanpur” ) ; } Output: I am in main Functions main( ) printf( ) scanf( ) getch( ) exit( ) gotorc( ) clrscr( ) for( ) while( ) if( ) switch( )
  • 234. Categories Functions Std. Library User-Defined printf( ) scanf( ) exit( ) clrscr( ) kanpur( ) bombay( ) gotorc( ) main( )
  • 235. Calling Functions main( ) { printf ( ”n I am in main” ) ; bombay( ) { printf ( ”n I am in Bombay” ) ; } kanpur( ) { printf ( ”n I am in Kanpur” ) ; } Output : I am in main I am in Bombay bombay( ) ; kanpur( ) ; I am in Kanpur } Function Call Function Def.
  • 236. Tips  A C program is nothing but a collection of 1 or more functions  If C program contains 1 function its name must be main( )  If C program contains more than 1 function then one of them has to be main( )  Execution of any C program always begins with main( )  Function names in a program must be unique
  • 237. Order, Order ! bombay( ) { printf ( ”n I am in Bombay” ) ; } main( ) { printf ( ”I am in main” ) ; bombay( ) ; } Tip: Functions can be defined in any order
  • 238. More Calls, More Bills main( ) { printf ( ”n I am in main” ) ; bombay( ) ; bombay( ) ; } bombay( ) { printf ( ”n I am in Bombay” ) ; } Tip: More the calls, slower the execution
  • 239. Nobody is Nobody’s Boss main( ) { kanpur( ) { printf ( ”n I am in Kanpur” ) ; bombay( ) ; } printf ( ”n I am in main” ) ; bombay( ) ; kanpur( ) ; } bombay( ) { printf ( ”n I am in Bombay” ) ; kanpur( ) ; } Tip: Any function can call any other function
  • 240. Local v/s STD v/s ISD Calls main( ) { printf ( ”n I am in main” ) ; main( ) ; } Local Call - Recursive Function Process - Recursion
  • 241. 115 - 120 Chapter 4
  • 242. Break From The Outermost Loop main( ) { for ( i = 1 ; i <= 22 ; i+= 2 ) { for ( j = 5 ; j <= 50 ; j++ ) { for ( k = 1 ; k <= 9 ; k += 3 ) { … … if ( i + j % k >= 2 ) break ; } } } } break ; break ; Go to work only once if ( i + j % k >= 2 ) break ;
  • 243. main( ) { for ( i = 1 ; i <= 22 ; i+= 2 ) { for ( j = 5 ; j <= 50 ; j++ ) { for ( k = 1 ; k <= 9 ; k += 3 ) { … … if ( i + j % k >= 2 ) goto out ; } } } } Better Way... out : ; Never use a goto
  • 244. main( ) { in : goto there ; for ( i = 1 ; i <= 22 ; i+= 2 ) { for ( j = 5 ; j <= 50 ; j++ ) { there : for ( k = 1 ; k <= 9 ; k += 3 ) { … … if ( i + j % k >= 2 ) } goto out ; } } } Where Am I... out : goto in ;
  • 245. Control Instructions  Sequence  Decision  Repitition  Case  goto
  • 246. Communication main( ) { int a = 10, b = 20, c = 30 ; calsum ( ) ; printf ( ”%d”, s ) ; } calsum( ) { int a, b, c, s ; } int s ; s = a + b + c ; printf ( ”%d”, s ) ; Garbage Garbage
  • 247. Passing Values main( ) { Actual Arguments int a = 10, b = 20, c = 30 ; int s ; calsum ( a, b, c ) ; printf ( ”%d”, s ) ; } calsum( ) { int s ; } int x, int y, int z s = x + y + z ; printf ( ”%d”, s ) ; Garbage Formal Arguments 60
  • 248. main( ) { Returning Values int a = 10, b = 20, c = 30, s ; calsum ( a, b, c ) ; } calsum ( int x, int y, int z ) { int ss ; ss = x + y + z ; return ( ss ) ; } s = calsum ( a, b, c ) ; printf ( ”%d”, s ) ; 60 return ; Returns only control Return control and value return ( ss ) ; return ( 60 ) ; return ( x + y + z ) ; 
  • 249. Are These Calls OK? calsum ( a, 25, d ) ; calsum ( 10 + 2, 25 % 3, d ) ; calsum ( a, calsum ( 25, 10, 4 ), d ) ; d = calsum ( a, 25, d ) * calsum ( a, 25, d ) + 23 ; calsum ( int x, int y, int z ) { int ss ; ss = x + y + z ; return ( ss ) ; }  Nested calls are legal. Call within an expression are legal.
  • 250. Returning More Than 1 Value main( ) { int a = 10, b = 20, c = 30 ; ssu, mp p= rsoudm( par,o bd, c( )a ;, b , c ) ; printf ( ”%d%d”, s, p ) ; } sumprod ( i n t x , i n t y , i n t z ) ss = x + y + z ; pp = x * y * z ; { int ss, pp ; return ( ss, pp ) ; } int s, p ;   A function can return only 1 value at a time
  • 251. main( ) { One More Try int a = 10, b = 20, c = 30 ; int s, p ; s = sumprod ( a, b, c ) ; p = sumprod ( a, b, c ) ; printf ( ”%d%d”, s, p ) ; } sumprod ( int x, int y, int z ) { ss = x + y + z ; pp = x * y * z ; return ( ss ) ; return ( pp ) ; } int ss, pp ; 60 60 Redundant
  • 252. main( ) { The Only Way Out int a = 10, b = 20, c = 30 ; int s, p ; s = sumprod ( a, b, c ) ; p = sumprod ( a, b, c ) ; printf ( ”%d%d”, s, p ) ; } , 1 , 2 sumprod ( int x, int y, int z, ) { int ss, pp ; ss = x + y + z ; pp = x * y * z ; } int code if ( code == 1 ) return ( ss ) ; else return ( pp ) ; Sum, Product, Average, Variance Standard Deviation
  • 253. A Better Way sumprod ( int x, int y, int z, int code ) { int ss, pp ; ss = x + y + z ; pp = x * y * z ; } if ( code == 1 ) return ( ss ) ; else return ( pp ) ; code == 1 ? return ( x + y + z ) : return ( x * y * z ) ; return ( code == 1 ? x + y + z : x * y * z ) ;
  • 254. ANSI V/s K & R calsum ( int x, int y, int z ) { .. } calsum ( x, y, z ) int x, y, z ; { .. } ANSI Kernighan & Ritchie
  • 255. main( ) { int y ; printf ( ”Enter year” ) ; scanf ( ”%d”, &y ) ; romanize ( y ) ; } romanize ( int yy ) { int n, i ; n = yy / 1000 ; for ( i = 1 ; i <= n ; i ++ ) printf ( ”m” ) ; Decimal Roman 1000 m 500 d 100 c 50 l 10 x 5 v 1 i } Roman Equivalent 1998 m d c c c c l x x x x i i i 1998 v
  • 256. A More General Call main( ) { int y ; printf ( ”Enter year” ) ; scanf ( ”%d”, &y ) ; , 1000 , ’m’ y = romanize ( y romanise ( y, 1000, ’) ; m’ ) ; } romanize ( intyy , int j , char ch ) { int n, i ; n = yy / j ; for ( i = 1 ; i <= n ; i ++ ) printf ( ) ; } ”%c”, ch return ( yy % j ) ;
  • 257. main( ) { ... 1998 md c c c c lx xx x v i ii , ’m’ y = romanise ( y, 1000 ) ; y = romanise ( y, 500, ’d’ ) ; y = romanise ( y, 100, ’c’ ) ; y = romanise ( y, 50, ’l’ ) ; y = romanise ( y, 10, ’x’ ) ; y = romanise ( y, 5, ’v’ ) ; romanise ( y, 1, ’i’ ) ; } romanize ( intyy ) { } , int j, char ch int n, i ; n = yy / j ; for ( i = 1 ; i <= n ; i ++ ) printf ( ”%c”, ch ) ; return ( yy % j ) ; Works
  • 258. Advanced Features of Functions  Returning a non-integer value  Call by value / Call by reference  Recursion
  • 259. Returning a Non-Integer Value main( ) { a = square ( 2.0 ) ; b = square ( 2.5 ) ; c = square ( 1.5 ) ; printf ( ” %f %f %f ”, a, b, c, ) ; square ( 2.0 ) ; } square ( f l o a t x ) { float y ; y = x * x ; printf ( ” %f ”, y ) ; return ( y ) ; } Function Prototype 4.0 6.0 2.0 float a, b, c ; float square ( float ) ; 4.0 6.25 2.25 float square ( float x )
  • 260. 156 - 161 (e)
  • 261. main( ) { What’s Wrong? float square ( float ) ; float a = 0.5 ; f ( a ) ; } f ( float x ) { float y ; y = square ( x ) ; printf ( ”%f”, y ) ; } float square ( float x ) { return ( x * x ) ; }
  • 262. What Would Be The Output main( ) { int a = 10 ; printf ( ”n a = %d” , a ) ; a = f( ) ; printf ( ”n a = %d” , a ) ; } a = 10 f( ) { printf ( ”Hello !” ) ; } a = 45
  • 263. main( ) { Solution 1 int a = 10 ; printf ( ”n a = %d” , a ) ; f( ) ; printf ( ”n a = %d” , a ) ; } a = 10 f( ) { printf ( ”Hello !” ) ; } a = 10 Returned value is ignored.
  • 264. main( ) { Solution 2 void f( ) ; int a = 10 ; printf ( ”n a = %d” , a ) ; f( ) ; printf ( ”n a = %d” , a ) ; } a = 10 void f( ) { printf ( ”Hello !” ) ; } a = 10 void prevents returning of value.
  • 265. Advanced Features of functions  Returning a non-integer value  Call by Value / Call by Reference  Recursion 
  • 266. Pointers * * The Biggest Hurdle !!
  • 267. Why Learn Pointers?  Simple  Powerful  Earn respect and money
  • 268. 10 4080 &i &i *( ) Memory Address Reference Memory location Cell number main( ) { Things Are Simple int i = 10 ; printf ( ”n value of i = %d ” , i ) ; printf ( ” n address of i = ” , ) ; %d printf ( ” n value of i = ” , ) ; } POINTER OPERATORS & - ‘Address Of’ Operator * - ‘Value at Address’ Operator - ‘Indirection’ Operator i 4080 %d 10 0 1 2 3 10  
  • 269. Would This Work... int i = 10 ; j = &i ; j = &23 ; j = & ( i + 34 ) ; & can be used only with a variable * can be used with variable, constant or expression printf ( “ %d ” *j ) ; printf ( “ %d ”, *4568 ) ; printf ( “ %d ”, * ( 4568 + 1 ) ) ; Some Change Necessary
  • 270. The Next Step int j ; int k ; printf ( ” n value of i = %d ” , i ) ; printf ( ” n address of i = %d ” , &i ) ; printf ( ” n value of i = %d ” , *( &i ) ) ; j = &i ; printf ( ” n address of j = % d ” , & j ) ; printf ( ” n value of j = % d ” , ) ; printf ( ” n value of j = ” , ) ; %d j *&j 10 4080 6010 k = &j ; printf ( ” n %d %d %d %d ” , k , &k , *k , *&k ) ; printf ( ” n %d %d %d %d %d %d ”, ) ; } i j k 4080 6010 5112 i, *&i, *j, **&j, **k, ***&k main( ) { int i = 10 ; 6010 4080 4080 6010 5112 4080 6010 * ** 10 10 10 10 10 10
  • 271. Kneel Down , Bow Your Head , Close Your Eyes ...
  • 272. In Essence ... & * - Address of operator - value of address operator Pointer Operators variable  *& variable Pointers are variables which hold addresses of other variables ? ? ? Integer Pointer int *j , ***l , ****m ; int i = 10 ; **k , m = &l ; j = &i ; k = &j ; l = &k ; int &k ; k = && j ;
  • 273. Accessing Screen if ( >= ’A’ && *s <= ’Z’ ) *(( s ( + s i ) + i ) >= ’A’ && **(s s+<= i) ’Z’ <= ) ’Z’ ) i)= *(s+i) + 32 ; ’a’ && *(s+i) <= ’z’ ) **(s = s+*i) s = - *s 32 - ; 32 ; main( ) { while ( 1 ) { } s = 1000 ; for ( i = 0 ; i <= 1999 ; i++ ) { } * s *s = *s + 32 ; else { if ( *s >= ’a’ && *s <= ’z’ ) } } 0xB8000000 int i ; char *s ; char far *s ; *(s+i) = *s + 32 ; *(s+i) >= *s <= ’z’ ) *(s+i) - 32 ; Screen M 1000 1000 +1 1000 +2 1000 +3 1000 + 1999 char ch ; ch = ’A’ ; ch = ch + 32 ;
  • 274. Accessing Screen main( ) { M + 2 + 4 + 6 0xB8000000 + 3998 int i ; char far *s ; i += 2 while ( 1 ) { } s = 0xB8000000 ; for ( i = 0 ; i <= 1999 ; i++ ) { } if ( * ( s + i ) >= ’A’ && *(s+i) <= ’Z’ ) *(s+i) = *(s+i) + 32 ; else { if ( *(s+i) >= ’a’ && *(s+i) <= ’z’ ) *(s+i) = *(s+i) - 32 ; } } 3999
  • 275. main( ) { char far *s ; int r ; char far * v ; s = 0xB8000000 ; for ( r = 1 ; r <= 24 ; r ++ ) { v = s + r * 160 ; * v = ch ; } } Screen ch = * s ; 0xB8000000 +160 +320 +480 +640 M +2 +4 +6 +3998 To Make Characters Fall char ch ;
  • 276. main( ) { Make All Characters Fall char far *s ; int r ; char far *v ; s = 0xB8000000 ; int c ; char ch ; for ( c = 0 ; c <= 79 ; c++ ) } } ch = * s 0xB8000000 Screen +160 +320 +480 +640 M +2 +4 +6 +3998 { + c * 2 ) ; ( for ( r = 1 ; r <= 24 ; r++ ) { v = s + r * 160 + c * 2 ; * v = ch ; }
  • 277. Any Screen Address Column 20 A 0xB8000000 Row 10 v = 0xB8000000 + 10 * 160 + 20 * 2 ; In general, v =0xB8000000 + r * 160 + c * 2 ; *v = ch ;
  • 278. main ( ) { Bells And Whistles char far *s ; char ch ; char far *v ; int r, c ; s = 0xB8000000 ; for ( c = 0 ; c <= 79 ; c++ ) { ch = *s ; for ( r = 1 ; r <= 24 ; r ++ ) { Millisec v = s + r * 160 + c * 2 ; *v = ch ; } } } What if 10 - 20? delay ( 60 ) ; * ( v - 160 ) = ’ ’ ; sound ( 350 ) ; delay ( 100 ) ; nosound ( ) ; 0xB8000000 Screen +160 +320 +480 +640 M +2 +4 +6 +3998 = * ( s + c * 2 ) ; r = random ( 2 4 ) ; c = random ( 79 ) ; Use # include ”time.h” randomize( )
  • 279. Are They Same char far *s ; char far *v ; char far *s, *v ; char far *s, far *v ;   char far *s ; s = 0xB8000000 ; char far *s = 0xB8000000 ;
  • 280. Why Two Bytes Apart main( ) { char far *s ; int i ; int color = 0 ; s = 0xB8000000 ; while ( 1) { } } for ( i = 1 ; i <= 3999 ; i += 2 ) * ( s + i ) = color ; color++ ; if ( color > 255 ) color = 0 ; Screen VDU Memory ASCII Color Color Byte 8 Bits Min value 00000000 Max value 11111111 Range 0 to 255 M +2 +4 +6 0xB8000000
  • 281. Is The Caps Lock On main( ) { kb = 0x417 ; *kb = 64 ; } char far *kb ; How would you put off the caps lock? Toggle Keys Caps lock Num lock Scroll lock Insert 01000000 7 6 5 4 3 2 1 0x417 0 Caps Lock 1 - On 0 - Off
  • 282. Don’t Do Delete 00001100 main( ) { char far *kb ; kb = 0x417 ; ‘ printf ( ”Please press delete key” ) ; *kb = 12 ; getch( ) ; } 7 6 5 4 3 2 1 0 Alt Ctrl Input Fun. scanf( ) getch( )
  • 283. Happy Birthday Joshi 7 6 5 4 3 2 1 0 0 0 Our Program Happy Birthday Joshi Del Ctrl + Alt + Del Ctrl + Alt + Del Del
  • 284. What would be the output main( ) { int i = 10 ; float a = 3.14 ; char ch = ’z’ ; j = &i ; b = &a ; dh = &ch ; k = &j ; c = &b ; eh = &dh ; printf ( ” %d %d %d ”, j , b , dh ) ; printf ( ” ”, *k ,*c , *eh ) ; %d %d %d i a ch j b dh 4000 5000 6000 4000 5000 6000 500 1500 2500 1000 2000 3000 printf ( ” ”, sizeof ( i ), sizeof ( a ), sizeof ( ch ) ) ; %d %d %d 10 3.14 z 1000 2000 3000 1000 2000 3000 k c eh 1000 2000 3000 2 4 1 Continued... int *j, **k ; float *b, **c ; char *dh, **eh ;
  • 285. i a ch j b dh 4000 5000 6000 4000 5000 6000 500 1500 2500 2 2 2 printf ( ” %d %d %d ”, sizeof ( j ), sizeof ( b ), sizeof ( dh ) ) ; printf ( ”%d %d %d”, sizeof ( k ), sizeof ( c ), sizeof ( eh ) ) ; printf ( ” %d %f %c ” , **k, **c, **eh ) ; } 10 3.14 z 1000 2000 3000 1000 2000 3000 k c eh 2 2 2 10 3.14 z ...Continued
  • 286. What would be the output main( ) { int i = 10 ; float a = 3.14 ; char ch = ’z’ ; j = &i ; b = &a ; dh = &ch ; k = &j ; c = &b ; eh = &dh ; printf ( ” %d %d %d ” , j , b , dh ) ; printf ( ” ” , *k ,*c , *eh ) ; %d %d %d i a ch j b dh 4000 5000 6000 4000 5000 6000 500 1500 2500 1000 2000 3000 printf ( ” ” , sizeof ( i ), sizeof ( a ), sizeof ( ch ) ) ; %d %d %d 10 3.14 z 1000 2000 3000 1000 2000 3000 k c eh 1000 2000 3000 2 4 1 Continued... int *j, **k ; float *b, **c ; char *dh, **eh ;
  • 287. i a ch j b dh 4000 5000 6000 4000 5000 6000 500 1500 2500 2 2 2 printf ( ”%d %d %d ”, sizeof ( j ), sizeof ( b ), sizeof ( dh ) ) ; printf ( ”%d %d %d”, sizeof ( k ), sizeof ( c ), sizeof ( eh ) ) ; printf ( ”%d %f %c” , **k, **c, **eh ) ; } 10 3.14 z 1000 2000 3000 1000 2000 3000 k c eh 2 2 2 10 3.14 z ...Continued
  • 288. Truly Speaking main( ) { int i = 25 ; float a = 3.14 ; j = &i ; int *j ; float *b ; b = &a ; } printf ( ”%d%f”, *j, *b ) ; i Binary of 25 401 401 402 a 501 502 503 504 j b Binary of 3.14 501
  • 289. Accessing Individual Bytes main( ) { float a = 3.14 ; float *b ; int *c ; char *d ; 501 503 504 a 502 b c d b = &a ; c = &a ; d = &a ; printf ( ” %f %d %d ”, *b, *c, *d ) ; printf ( ”%d %d %d %d”, *d, *(d+1), *(d+2), *(d+3) ) ; printf ( ”%d %d”, *( c+1 ), *( c+2 ) ) ; } 501 0’s & 1’s 0’s & 1’s 0’s & 1’s 0’s & 1’s 501 501
  • 290. main( ) { int i = 25 ; int *j ; float a = 3.14 ; float *b ; char far *kb ; kb = 0x417 ; j = &i ; b = &a ; } Why far and near
  • 291. i a j b kb 25 3.14 200 300 200 300 0x417 Code Segment Turbo C DOS/Windows BDA 0x417 IVT 0xB8000000 Screen M A 0xB8000000 M A B C D E F 384 KB High Memory 640 KB Base or Conventional Memory  Data Segment Free Memory (640 + 64 ) * 1024 HEX 64 * 6
  • 292. main( ) { Pointer Sizes int i = 25 ; int *j ; float a = 3.14 ; float *b ; char far *kb ; kb = 0x417 ; j = &i ; b = &a ; } 2 2 printf ( ”%d %d %d”, sizeof ( j ), sizeof ( b ), sizeof ( kb ) ) ; 4
  • 293. How Much Memory main( ) { int far *m ; m = 0x413 ; printf ( ”%d” ,*m ) ; } 0x413 0x414 640 640 KB BDA Ideally 632 634 636 ?
  • 294. main( ) { int a = 10, b = 20 ; printf ( ”%d%d”, a, b ) ; swapv ( a , b ) ; printf ( ”%d%d”, a, b ) ; } swapv ( int x, int y ) { } int t ; t = x ; x = y ; y = t ; printf ( ”%d%d”, x, y ) ; 10 20 10 20 a b x y t x y t 10 20 g 10 20 10 x y t 20 20 10 x y t 20 10 10 20 10 10 20
  • 295. swapr ( & a , & b ) ; printf ( ”%d%d”, a, b ) ; swapr ( ) { } int t ; t = *x ; *x = *y ; *y = t ; 10 20 20 10 a b 20 20 10 300 x y t main( ) { int a = 10, b = 20 ; printf ( ”%d%d”, a, b ) ; int *x , int *y 200 } 10 200 300 10
  • 296. main( ) { int a = 10, b = 20, c = 30 sumprod ( a, b, c, ) ; printf ( ”%d%d”, s, p ) ; } , s, p ; 60 6000 sumprod ( int x, int y, int z, ) { } x + y + z ; x * y * z ; &s, &p int *ss, int*pp ss = pp = * * a b c s p 6G0 60G00 100 200 10 20 30 x y z ss pp 10 20 30 100 200
  • 297. Advanced Features of Functions • Returning Non-integer Value • Call By Value / Reference • Recursion
  • 298. To Be A Successful Software Developer Creative Intelligence Analytical Ability Patience Ability To Think
  • 299. Simpler Form main( ) { printf ( ”Hi” ) ; main( ) ; }
  • 300. One More Form main( ) { f( ) ; } f( ) { printf ( ”Hi” ) ; f( ) ; }
  • 301. More General main( ) { int num, sum ; printf ( ”Enter a number” ) ; scanf ( ”%d”, &num ) ; } sumdig ( int n ) { int d ; int s = 0 ; while ( n != 0 ) { d = n % 10 ; n = n / 10 ; s = s + d ; } return ( s ) ; } 31698 d5 372 d3 2846 d4 n s d 327 0 7 32 7 2 3 9 3 0 12 327 sum = sumdig ( num ) ; printf ( ”%d”, sum ) ;
  • 302. main( ) { printf ( ”Enter a number” ) ; scanf ( ”%d”, &num ) ; } int num, sum ; rsum ( int n ) { int d ; int s ; if ( ) { n != 0 d = n % 10 ; s = d + rsum ( n ) ; n = n / 10 ; } else return ( 0 ) ; } return ( s ) ; 327 sum = rsum ( num ) ; printf ( ”%d”, sum ) ;
  • 303. rsum ( int n ) { if ( ) { n != 0 d = s = rsum ( int n ) { if ( ) { n != 0 d = n = s = } else return ( 0 ) ; return ( s ) ; } rsum ( int n ) { if ( ) { n != 0 d = n = s = } else return ( 0 ) ; return ( s ) ; } rsum ( int n ) { if ( ) { n != 0 d = s = n = } else return ( 0 ) ; return ( s ) ; } 327 % 10 ; 327 / 10 ; 7 + rsum ( 32 ) ; n = } else return ( 0 ) ; return ( s ) ; } 32 % 10 ; 32 / 10 ; 2 + rsum ( 3 ) ; 3 % 10 ; 3 / 10 ; 3 + rsum ( 0 ) ; 327
  • 304. Factorial Value main( ) { printf ( ”Enter a number” ) ; scanf ( ”%d”, &num ) ; } int num, fact ; fact = factorial ( num ) ; printf ( ”%d”, fact ) ; factorial ( int n ) { int p = 1 ; while ( ) { } n != 0 p = p * n ; n-- ; return ( p ) ; }
  • 305. Recursive Factorial main( ) { printf ( ”Enter a number” ) ; scanf ( ”%d”, &num ) ; } int num, fact ; fact = refact ( num ) ; printf ( ”%d”, fact ) ; refact ( int n ) { int p ; if ( n ! = 0 ) p = n * refact ( n - 1 ) ; return ( p ) ; } else return ( 1 ) ;
  • 306. Advantages of Recursion Ease ? Speed ?    
  • 307. Peg A Peg B Peg C A B C A to C A B C A B C A to B A B C A B C C to B A to C B to A B to C A to C 3 ! + 1 A B C A B C
  • 310. int i ; short int i ; default signed short int i ; i Type of Integers int short int long int signed short unsigned short Specifier: %u Specifier: %i %d %o %x %X 15 bits = value max value 111 1111 1111 1111 32767 Sign bit 0 - Positive 1 - Negative Range:-32768 to +32767 Size: 2 Bytes Range: 0 to 65535 Size: 2 Bytes
  • 311. Type of Integers Sign bit 0 - Positive int 1 - Negative 111 1111 1111 1111 1111 1111 1111 1111 short int long int signed unsigned Specifier: %ld long int a 31 bits = value max value 2147483647 long signed long unsigned Specifier: %lu Range: -2147483648 to +2147483647 Size: 4 Bytes Range: 0 to 4294967295 Size: 4 Bytes
  • 312. Type of Chars char char ch ; Sign bit 7 bits = value 0 - Positive 1 - Negative signed char unsigned char Specifier: %c Specifier:%c max value 111 1111 127 Range : -128 to + 127 Size : 1 Byte Range: 0 to 255 Size: 1 Byte
  • 313. Types of Real Real float double long double Range: -3.4*1038 to +3.4*1038 Size: 4 bytes Specifier: %f Range: -1.7*10308 to +1.7*10308 Size: 8 bytes Specifier: %lf Range: -1.7*104932 to +1.7*104932 Size: 10 bytes Specifier: %Lf 1 - 8 - 23 1 - 11 - 52 1 - 15 - 64 127 1023 16383
  • 314. int short int long int signed unsigned signed unsigned char Real signed unsigned float double long double
  • 315. Chars With a Sign? char ch = ’A’ ; ch 65 char dh = -15 ;  dh -15
  • 316. What If We Exceed The Range? signed char ch = 128 ; printf ( ”%d”, ch ) ; -128 Why?
  • 317. Why This Bias? char -128 +127 int -32768 +32767 long int -2147483648 +2147483647
  • 318. signed char ch = -128 ; Binary of 128 10000000 1’s complement 01111111 2’s complement 10000000 printf ( ”%d”, ch ) ; -128 unsigned char dh = 128 ; signed char eh = 128 ; Binary of 128 10000000 printf ( ”%u”, dh ) ; Binary of 128 10000000 Add 0 for Positive Sign bit 010000000 Store rightmost 8 bits 10000000 printf ( ”%d”, eh ) ; 01111111 +1 10000000 -128 128
  • 319. Various Forms signed short int i ; short signed int i ; short int i ; signed int i ; int i ; short i ; signed i ; unsigned short int j ; short unsigned int j ; unsigned int j ; unsigned j ; long signed int k ; signed long int k ; long int k ; long k ; long unsigned int i ; unsigned long int i ; unsigned long i ; signed char ch ; char signed ch ; char ch ; unsigned char dh ; float a ; long double e ; char unsigned dh ; double d ;
  • 320. What Is 365? int 365 long int unsigned int unsigned long int 365L 365l 365u 365lu 365ul What Is 3.14? double 3.14 float long double 3.14f 3.14L
  • 321. Would This Work main( ) { int i ;   for ( i = 0 ; i <= 255 ; i++ ) printf ( ”%d %c”, i, i ) ; } char i ; unsigned char i ;
  • 322. Stick To Your Guns main( ) { for ( i = 0 ; i < 255 ; i++ ) printf ( ”%d %c”, i, i ) ; } unsigned char i ; printf ( ”%d %c”, i, i ) ;
  • 323. Storage Classes In C A Complete definition of variable : Type Storage Class
  • 324. A Storage Class signifies Storage Default Initial Value Scope Life
  • 325. Types of Storage Classes Automatic Register Static External
  • 326. Automatic Storage Class Storage Default Initial Value Scope Life Memory Garbage Local to the block in which the variable is defined Till the control is in the block in which the variable is defined
  • 327. Automatic Storage Class main( ) { int a ; static int b ; automatic int c ; printf ( ”%d %d %d”, a, b, c ) ; automatic auto } Error ?  
  • 328. Default Initial Value main( ) { int a ; static int b ; auto int c ; printf ( ”%d%d%d”, a, b, c ) ; } G 0 G
  • 329. Which Is Correct? main( ) { int a = 10 ; float a = 3.14 ; . . . . . . . . . . . . . . } main( ) { int a = 10 ; int a = 20 ; . . . . . . . . . . . . . .  }  Tip: Redefinition not allowed
  • 330. Redefinition? main( ) { int a = 10 ; { int a = 20 ; { int a = 30 ; printf ( ”%d”, a ) ; } printf ( ”%d”, a ) ; } printf ( ”%d”, a ) ; } Error ! Why? main ( ) { int a = 20 ; f ( ) ; } f ( ) { printf ( ”%d”, a ) ; } 30 20 10
  • 331. Death, But When? main( ) { Won’t die when the control goes to f( ) int i =10, j = 20, k, l ; k = f ( ) ; l = i + j + k ; printf ( ”%d”, l ) ; } f ( ) { int m = 5, n ; n = m * 2 ; return ( n ) ; } Would die when control goes back
  • 332. Register Storage Class Storage : Default Initial value : Scope : Life : CPU Registers 14 Each of 2 bytes Central Processing Unit Microprocessor μp
  • 333. Different Speeds? main( ) { auto int i ; register int i ; for ( i = 1 ; i <= 250 ; i++ ) printf ( ”%d %c”, i, i ) ; } main( ) { for ( i = 1 ; i <= 250 ; i++ ) printf ( ”%d %c”, i, i ) ; } Register Storage Class Storage : CPU Registers Default Initial Value : Garbage Scope : Local to the block in which the variable is defined Life : Till the control remains in the block in which the variable is defined
  • 334. Be Judicious main( ) { register int i ; register int j = 20 ; for ( i = 1 ; i <= 250 ; i++ ) printf ( ”%d %c”, i, i ) ; printf ( ”%d”, j ) ; }
  • 335. Static Storage Class Storage Default Initial Value Scope Life Memory 0 Local to the block in which the variable is defined Variable persists between different function calls
  • 336. main( ) { increment( ) ; increment( ) ; increment( ) ; } increment( ) { auto int i = 1 ; register int j = 1 ; static int k = 1 ; i++ ; j++ ; k++ ; printf ( ”%d %d %d”, i, j, k ) ; 2 2 2 2 2 3 2 2 4 }
  • 337. When to Use Static main( ) { f( ) f( ) ; p = int a = 20 ; return ( &a ) ; } printf ( ”%d”, *p ) ; } int *p ; int * f( ) ; { a p 20 250 250 int * f( ) char ** f ( int *, float * ) ; static int a = 20 ;
  • 338. External Storage Class main( ) { printf ( ”%d”, a ) ; increment( ) ; increment( ) ; decrement( ) ; printf ( ”%d”, a ) ; } increment( ) { a++ ; printf ( ”%d”, a ) ; } decrement( ) { a-- ; printf ( ”%d”, a ) ; } 10 11 12 11 11 int a = 10 ; Output
  • 339. Declaration V/s Definition main( ) { extern int a ; printf ( ”%d”, a ) ; increment( ) ; increment( ) ; decrement( ) ; printf ( ”%d”, a ) ; } increment( ) { extern int a ; Declaration } a++ ; printf ( ”%d”, a ) ; decrement( ) { extern int a ; } a-- ; printf ( ”%d”, a ) ; int a = 10 ; Declaration Declaration Definition float square ( float ) ; float square ( float ) { .. .. } Definition
  • 340. Declaration V/s Definition main( ) { extern int a ; printf ( ”%d”, a ) ; increment( ) ; increment( ) ; decrement( ) ; printf ( ”%d”, a ) ; } increment( ) { extern int a ; a++ ; printf ( ”%d”, a ) ; int a = 10 ; decrement( ) { extern int a ; } a-- ; printf ( ”%d”, a ) ; }
  • 341. Declaration V/s Definition main( ) { extern int a ; printf ( ”%d”, a ) ; increment( ) ; increment( ) ; decrement( ) ; printf ( ”%d”, a ) ; int a = 10 ; increment( ) { extern int a ; decrement( ) { extern int a ; } a++ ; printf ( ”%d”, a ) ; } a-- ; printf ( ”%d”, a ) ; }
  • 342. Two Types Of Conflicts int a = 10 ; main( ) { int a = 20 ; { int a = 30 ; printf ( ”%d”, a ) ; } printf ( ”%d”, a ) ; } printf ( ”%d”, a ) ; 30 20
  • 343. External Storage Class Storage Default Initial Value Scope Life Memory 0 Global Till execution of the program doesn’t end
  • 344. Which is The Most Powerful  Automatic  Register  Static  External All other cases For frequently used variables If variable is to live across function calls If variable required by all functions
  • 345. Ctrl F9 F9 F7 F8 C Preprocessor Compile and Execute Compile Step into Step over
  • 346. main( ) { int a = 10 ; float b = 3.5 ; display( ) ; printf ( ”Hello” ) ; } display( ) { printf ( ”Nagpur” ) ; }
  • 347. A Closer Look Hand written program Text Editor C Source Code Preprocessor Expanded source code Compiler Object code Linker Executable code C : Helps in typing TC WS4 . . a program WORKS Expands the source code Converts expanded source code into machine language
  • 348. Why IDE  Editor  Preprocessor  Compiler  Linker Ctrl F9 Turbo C/C++ Preprocesses, Compiles, Links and Executes
  • 349. Types of Preprocessor Directives File Inclusion Macro Expansion Conditional Compilation Miscellaneous Directives
  • 350. Preprocessor In Action # include ”goto.c” main( ) { Compiler clrscr( ) ; gotorc ( 10, 20 ) ; printf ( ”Hello !” ) ; } Source Code Ctrl F9 Preprocessor main( ) { clrscr( ) ; gotorc ( 10, 20 ) ; printf ( ”Hello !” ) ; } gotorc( - , - ) { --- } Expanded source code
  • 351. Unresolved Externals gotorc( - , - ) { main( ) { --- } --- } Compiler machine language code of gotorc( ) machine language code of main( ) Error Why?
  • 352. Solution m/c language code of gotorc( ) m/c language code of main( ) m/c language code of clrscr( ) m/c language code of printf( ) Linker Executable code
  • 353. Your wish # include ”goto.c” # include <goto.c> C : TC WS4 . . WORKS INCLUDE INCLUDE DIRECTORIES C: TC INCLUDE C: TC LIB C: TC WORKS C: TC LIBRARY DIRECTORIES OUTPUT DIRECTORY SOURCE DIRECTORIES
  • 355. 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 = 1 ; i <= 10 ; i++ )
  • 356. Macro Expansion # define PI 3.14 main( ) { float r, a ; printf ( ”Enter radius” ) ; scanf ( ”%f”, &r ) ; a = PI * r * r ; printf ( ”n%f”, a ) ; } a = 3.14 * r * r ; 3.141528   # define PI 3.14 PI = 6.28 ; float PI = 3.14 ; PI = 6.28 ;
  • 357. 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
  • 358. Types Of Macros Macros With Arguments Simple Macros
  • 359. 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 ) ; Order is unimportant float area ( float rr ) ; } float area ( float rr ) { return ( PI * rr * rr ) ; } a = 3.14 * r * r ; Macros - Faster Functions - Less Space
  • 360. Macros With Arguments # define S ( x ) x * x main( ) { 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 ) ; } j = 2 + 2 * 2 + 2 ; k = 3 + 1 * 3 + 1 ; i = 1 + 3 * 1 + 3 ; i = 4 * 4 ; m = ++n * ++n ; 16 8 7 7 4 16
  • 361. Conditional Compilation main( ) { scanf ( .. ) ; /* input */ .. .. /* formula */ .. } .. .. .. /* */
  • 362. Conditional Compilation main( ) { scanf ( .. ) ; /* input */ .. .. /* formula */ .. } .. .. .. # ifdef YES # endif 2 Solutions : - # define YES - Delete # ifdef, # endif
  • 363. Miscellaneous Directives # define YES 10 Redefinition main( ) { a = YES - .. ; printf ( ”YES” ) ; } f( ) { int YES ; } a = YES + .. ; # define YES 20 # undef YES OK Never Replaced
  • 364. # pragma # pragma inline main( ) { asm stc asm pushf asm pop flags .. } .. .. .. .. F7, F8, F9, Ctrl F9 Won’t Work
  • 365. How much C 10 %? 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 27 / 32
  • 366. 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 ) ;
  • 367. 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
  • 368. 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 per ( i ) per [ i ] H2O  
  • 369. 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    
  • 370. 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
  • 371. 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?
  • 372. 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
  • 373. 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
  • 374. 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
  • 375. 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 6 17 13 12 2 2 17 13 12 6 2 1 - 2 2 2 2 6 2 6 2 6 12 0 - 2 0 - 3 0 - 4 13 17 12 6 12 17 13 6 1 - 3 6 17 13 12 1 - 4 13 17 12 12 17 13 2 - 3 2 - 4 13 17 3 - 4
  • 376. 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
  • 377. Sorting Procedures  Selection Sort  Bubble Sort  Shell Sort  Shuttle Sort  Heap Sort  Merge Sort  Radix Sort  Quick Sort  qsort( ) Recursion Fastest? n2 log2n
  • 378. 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
  • 379. Chapter 7 Pg 209-212 244-249 (C) Chapter 7
  • 380. main( ) { int a[ ] = { 7, 9, 16, -2, 8 } ; a[0] a[1] a[2] a[3] a[4] 7 9 16 -2 8 102 104 106 108 110 int i ; printf ( ”%u %u %u”, &a[ 0 ], &a[ 1 ], &a[ 2 ] ) ; printf ( ”%u %u %u”, a, a + 1, a + 2 ) ; printf ( ”%d %d %d”, *a, *( a +1 ), *( a + 2 ) ) ; for ( i = 0 ; i <= 4 ; i ++ ) printf ( ”%d”, *( a + i ) ) ; for ( i = 0 ; i <= 4 ; i ++ ) printf ( ”%d”, a[ i ] ) ; } 102 104 106 102 104 106 7 9 16 Another Form... Subscript Notation Pointer Notation
  • 381. More Forms... main( ) { int a[ ] = { 7, 9, 16, -2, 8 } ; int i ; for ( i = 0 ; i <= 4 ; i ++ ) printf ( ”%d”, a[ i ] ) ; for ( i = 0 ; i <= 4 ; i ++ ) printf ( ”%d”, *( a + i ) ) ; for ( i = 0 ; i <= 4 ; i ++ ) printf ( ”%d”, *( i + a ) ) ; for ( i = 0 ; i <= 4 ; i ++ ) printf ( ”%d”, i[ a ] ) ; } a[ i ]i[ a ] *( a + i ) *( i + a )
  • 382. Flexible Arrays int a[ n ] ; scanf ( ”%d”, &n ) ; Size of an array must always be mentioned as a positive, non-zero, integer constant
  • 383. Pointer Arithmetic a b 1008 2009 6002 1012 main( ) { float a = 3.14 , *b ; char ch = ’z’ , *dh ; int i = 25 , *j ; b = &a ; dh = &ch ; j = &i ; printf ( ”%u %u %u”, b, dh, j ) ; b++ ; dh++ ; j++ ; printf ( ”%u %u %u”, b, dh, j ) ; b += 3 ; dh += 8 ; j -= 3 ; printf ( ”%u %u %u”, b, dh, j ) ; } 3.14 ch i z 25 1008 2009 6002 1008 dh j 2009 6002 1024 2010 6004 2018 5998
  • 384. Legal Pointer Arithmetic Pointer + number Pointer - number Pointer - Pointer Pointer Pointer Number
  • 385. Access Using Pointers main( ) { int a[ ] = { 7, 9, 16, -2, 8 } ; int *p ; int i ; p = a ; /* same as &a[ 0 ] */ printf ( ”%d”, *p ) ; p++ ; 7 printf ( ”%d”, p ) ; 104 printf ( ”%d”, *p ) ; 9 for ( i = 0 ; i <= 4 ; i++ ) printf ( ”%d”, *p ) ; p++ ; { } } a[0] a[1] a[2] a[3] a[4] 7 9 16 -2 8 102 104 106 108 110 p p 102 104 p++ * p Ist * ++p ; IInd Ist p++ ; ++ IInd ++ *p ; p = a ; 
  • 386. main( ) { [ ] For Notation... int a[ ] = { 7, 9, 16, -2, 8 } ; int *p ; int i ; p = a ; for ( i = 0 ; i <= 4 ; i ++ ) printf ( ”%d %d %d %d”, } a[0] a[1] a[2] a[3] a[4] 7 9 16 -2 8 102 104 106 108 110 *( p + i ), *( i + p ), p[ i ], i[ p ] ) ; int a[5] ; int 5[a] ; printf ( ”%d”, a[2] ) ; printf ( ”%d”, 2[a] ) ;  
  • 387. Dancing Dolls Revisited main( ) { char far *s = 0xB8000000 ; int i ; for ( i = 0 ; i <= 3999 ; i += 2 ) { if ( *( s + i ) >= ’A’ && *( s + i ) <= ’Z’ ) } } s[ i ] * ( i + s ) i[ s ] a[i] - One is pointer or array - Other is int
  • 388. main( ) { Changing Array Address int a[ ] = { 7, 9, 16, -2, 8 } ; int *p ; int i ; p = a ; for ( i = 0 ; i <= 4 ; i ++ ) printf ( ”%d”, *p ) ; p++ ; { } } for ( i = 0 ; i <= 4 ; i ++ ) printf ( ”%d”, *a ) ; a++ ; { } Error a[0] a[1] a[2] a[3] a[4] 7 9 16 -2 8 102 104 106 108 110 a-- ; a = a + 2 ; a = a - 2 ; a += 2 ; a -= 2 ;
  • 389. main( ) { Passing Array Elements int a[ ] = { 7, 9, 16, -2, 8 } ; int i ; display ( a[ 0 ], a[ 1 ], a[ 2 ], a[ 3 ], a[ 4 ] ) ; for ( i = 0 ; i <= 4 ; i++ ) display1 ( a[ i ] ) ; } display( int i, int j, int k, int l, int m ) { printf ( ”%d %d %d %d %d”, i, j, k, l, m ) ; } display1 ( int n ) { printf ( ”%d”, n ) ; } Which is good?
  • 390. main( ) { Passing Entire Array int a[ ] = { 7, 9, 16, -2, 8 } ; display2 ( a ) ; display3( a , sizeof ( a ) / 2 - 1 ) ; } display2 ( i n t * p ) int i ; for ( i = 0 ; i <= 4 ; i ++ ) printf ( ”%d”, ) ; } *( p + i ) { display3 ( i n t * p , int n ) { i <= n int i ; for ( i = 0 ; i <= 4 ; i ++ ) } printf ( ”%d”, * ( p + i ) ) ; a[0] a[1] a[2] a[3] a[4] 7 9 16 -2 8 102 104 106 108 110 qsort ( a, 5, , , ) ;
  • 391. Remember... Any time an entire array is to be passed to function, it is necessary to pass (1) Base address of array (2) No. of elements present in the array
  • 392. main( ) Two Dimensional Array { int a[ ][ ] = { { 2, 6, 1, 8, 4 } { 1, 2, 5, 6, 8 } { 7, 9, 8, 7, 21 } { 4, 5, 6, 8, 10 } } ; optional int i, j ; printf ( ”%d”, a[ ][ ] ) ; 2 4 printf ( ”%d %d”, sizeof ( a ), a ) ; for ( i = 0 ; i <= 3 ; i++ ) { for ( j = 0 ; j <= 4 ; j++ ) printf ( ”%d”, a [ i ][ j ] ) ; } printf ( ”n” ) ; } 5 optional int b[ ][1][2][3] compulsory 21 40 4080 optional , , , compulsory compulsory Exception
  • 393. main( ) { int a[ ][ ] = { Find Biggest... ,, 7, 2, 6, 1 3, 5, 4, 8 6, 2, 9, 50 1, 2, 3, 8 } ; , int i, j, big ; int r, c ; big = a[ 0 ][ 0 ] ; for ( i = 0 ; i <= 3 ; i++ ) { for ( j = 0 ; j <= 3 ; j++ ) { if ( a[ i ][ j ] > big ) big = a[ i ][ j ] ; { r = i ; c = j ; } } } printf ( ”%d ”, big ) ; printf ( ”%d %d ”, r, c ) ; } 4 r = 0 ; c = 0 ; Find Second Biggest and its Position
  • 394. main( ) { int a[ ][ ] = { 4 Row Major 7, 2, 6, 1, 9, 3, 4, 5, 10, 12, 16, 18 } ; 7 2 6 1 9 3 4 5 10 12 16 18 502 504 506 508 510 512 514 516 518 520 522 524 printf ( ”%d ”, a ) ; printf ( ”%d ”, *a ) ; printf ( ”%d %d %d ”, a+0 , a+1 , a+2 ) ; printf ( ”%d %d %d ”, *(a+0) , *(a+1) , *(a+2) ) ; printf ( ”%d %d %d ”, a[0] , a[1] , a[2] ) ; printf ( ”%d %d %d ”, a[0]+1 , a[1]+2 , a[2]+3 ) ; printf ( ”%d %d %d ”, *(a[0]+1) , *(a[1]+2) , *(a[2]+3) ) ; printf ( ”%d %d %d ”, a[0][1] , a[1][2] , a[2][3] ) ; 502  7  502  504  506  7  2  6  7  2  6 8  4  9  Garbage/ Error 2 4 } 18
  • 395. int a[ ][ 4 ] = { 7, 2, 6, 1 , 9, 3, 4, 5, 10, 12, 16, 18 } ; 7 2 6 1 9 3 4 5 10 12 16 18 502 510 518 printf ( ”%d”, a ) ; printf ( ”%d”, *a ) ; 502 int b[ ] = { 7, 2, 6, 1 } ; 7 2 6 1 402 printf ( ”%d”, b ) ; printf ( ”%d”, *b ) ; 402 7 502 5 int i = 5 ; printf ( ”%d”, i ) ; i 5 200
  • 396. main( ) { int a[ ][ ] = { 4 Row Major 7, 2, 6, 1, 9, 3, 4, 5, 10, 12, 16, 18 } ; 7 2 6 1 9 3 4 5 10 12 16 18 502 510 514 518 524 printf ( ”%d ”, a ) ; printf ( ”%d ”, *a ) ; printf ( ”%d %d %d ”, a+0 , a+1 , a+2 ) ; printf ( ”%d %d %d ”, *(a+0) , *(a+1) , *(a+2) ) ; printf ( ”%d %d %d ”, a[0] , a[1] , a[2] ) ; printf ( ”%d %d %d ”, a[0]+1 , a[1]+2 , a[2]+3 ) ; printf ( ”%d %d %d ”, *(a[0]+1) , *(a[1]+2) , *(a[2]+3) ) ; printf ( ”%d %d %d ”, a[0][1] , a[1][2] , a[2][3] ); 502 502 502 510 518 502 510 518 502 510 518 504 514 524 2 4 18 2 4 } 18
  • 397. Moral... a[2][3] * ( a[2] + 3 ) * ( * ( a + 2 ) + 3 ) Array 1-D 2-D 3-D 4-D Subscript Pointer a[ i ] *( a+i ) a[ i ][ j ] *( *( a+i ) + j ) a[ i ][ j ][ k ] *( *( *(a + i ) + j ) + k ) a[ i ][ j ][ k ][ l ] *( *( *( *(a + i ) + j ) + k )+ l )
  • 398. 3 Ways main( ) { int a[ ][ 4 ] = { 7, 2, 6, 1, 9, 3, 4, 5, 10, 12, 16, 18 } ; int i, j ; for ( i = 0 ; i <= 2 ; i++ ) { for ( j = 0 ; j <= 3 ; j++ ) printf } } a[i][j][k] *( a[i][j] + k ) *( *( a[i] ) + j ) + k ) *( *( *(a + i ) + j ) + k ) ( ”%d %d %d”, a[ i ][ j ] , *( a[ i ] + j ) , *( *( a + i ) + j ) ) ;