C/C++
Table of contents 1. 2. 3. 4. C C++ 5. 6. 7. 1
i // shoe1.c: # include <stdio.h> # define ADJUST 7. 64 # define SCALE 0. 325 int main ( void ) { double shoe, foot ; shoe = 9. 0; foot = SCALE * shoe + ADJUST ; printf (" Shoe size ( men \ s) foot length \n"); printf (" %10.1 f %15.2 f inches \n", shoe, foot ); return 0; } 2
Shoe size ( men s) foot length 9.0 10. 56 inches 3
i // shoe2.c: # include <stdio.h> # define ADJUST 7. 64 # define SCALE 0. 325 int main ( void ) { double shoe, foot ; printf (" Shoe size ( men \ s) foot length \n"); shoe = 8. 0; while ( shoe < 18. 5) { foot = SCALE * shoe + ADJUST ; printf (" %10.1 f %15.2 f inches \n", shoe, foot ) ; shoe = shoe + 1; 4
ii } } printf ("If shoes fit, wear it.\n"); return 0; 5
Shoe size ( men s) foot length 12. 0 11. 54 inches 13. 0 11. 87 inches 14. 0 12. 19 inches 15. 0 12. 52 inches 16. 0 12. 84 inches 17. 0 13. 16 inches 18. 0 13. 49 inches If shoes fit, wear it. 6
while while ( condition ) statement while ( condition ) { statements } while ( condition ){ statements } 7
while shoe = 10.0; no go to next statement shoe < 18.5 yes foot = SCALE * shoe + ADJUST; printf("..."); shoe = shoe + 1; 8
int n; n = 2016; 9
int n; n = 2016; C = (assignment operator) 2016 n n 2016 9
i = i + 1; 10
i = i + 1; i 22 i = 22 + 1 i 23 10
2016 = n; 11
1. 2. 3. 12
// AssignOpThree.c: # include <stdio.h> # include < string.h> int main ( void ) { } int a, b, c; a = b = c = 10; printf ("a = %d, b = %d, c = %d\n", a, b, c); return 0; 13
// AssignOpThree.c: # include <stdio.h> # include < string.h> int main ( void ) { } int a, b, c; a = b = c = 10; printf ("a = %d, b = %d, c = %d\n", a, b, c); return 0; a = 10, b = 10, c = 10 13
// AssignOpThree.c: # include <stdio.h> # include < string.h> int main ( void ) { } int a, b, c; a = b = c = 10; printf ("a = %d, b = %d, c = %d\n", a, b, c); return 0; a = 10, b = 10, c = 10 10 c c b b a 13
(addition operator) printf ("%d", 4+20) ; c = a + b; 14
(subtraction operator) b = 20. 0-200. 0; + - 15
+ - a = -1; b = -a; - C99 + a = +1; 16
mile = 1.6 * km; C 17
// square.c: # include <stdio.h> int main ( void ) { int num =1; while ( num < 10) { printf ("%4d^2 = %6d\n", num, num * num ); num = num + 1; } return 0; } 18
1^2 = 1 2^2 = 4 3^2 = 9 4^2 = 16 5^2 = 25 6^2 = 36 7^2 = 49 8^2 = 64 9^2 = 81 19
// divide.c: # include <stdio.h> int main ( void ) { printf (" 3/4 = %d\n", 3/4) ; printf (" 6/3 = %d\n", 6/3) ; printf (" 7/4 = %d\n", 7/4) ; printf (" 7./4. = %.2 f\n", 7./4.) ; printf (" 7./4 = %.2 f\n", 7./4) ; return 0; } 20
3/4 = 0 6/3 = 2 7/4 = 1 7./4. = 1.75 7./4 = 1.75 21
C 22
( ) +, -( ) *, / +, -( ) = 23
y = 6 * 12 + 5 * 20; 24
y = 6 * 12 + 5 * 20; C 24
y = 12 / 3 * 2; 25
y = 12 / 3 * 2; / * 3 12/3 25
// rules.c: # include <stdio.h> int main ( void ) { int a, b; b = a = -(2 + 5) * 6 + (4 + 3 * (2 + 3)); printf ("b = %d\n", b); return 0; } 26
// rules.c: # include <stdio.h> int main ( void ) { int a, b; b = a = -(2 + 5) * 6 + (4 + 3 * (2 + 3)); printf ("b = %d\n", b); return 0; } b = -23 26
// rules.c: # include <stdio.h> int main ( void ) { int a, b; b = a = -(2 + 5) * 6 + (4 + 3 * (2 + 3)); printf ("b = %d\n", b); return 0; } b = -23 1. b = a = -(2 + 5) * 6 + (4 + 3 * (2 + 3)); 2. b = a = -7 * 6 + (4 + 3 * (2 + 3)); 3. b = a = -7 * 6 + (4 + 3 * 5); 4. b = a = -7 * 6 + (4 + 15) ; 5. b = a = -7 * 6 + 19; 6. b = a = -42 + 19; 7. b = a = -23; 8. b = -23; 26
%
% (modulus operator) 27
% // modulus.c: # include <stdio.h> int main ( void ) { printf ("13 %% 5 = %d\n", 13 % 5); return 0; } 13 % 5 = 3 28
% i // min_sec.c: # include <stdio.h> # define SEC_ PER_ MIN 60 int main ( void ) { int sec, min, left ; printf (" Convert secs to mins and secs!\n"); printf (" Enter the number of secs ( <=0 to quit ) :\n"); scanf ("%d", & sec ); while (sec >0) { min = sec / SEC_ PER_ MIN ; left = sec % SEC_ PER_ MIN ; 29
% ii } printf ("%d secs is %d mins, %d secs.\n", sec, min, left ); printf (" Enter next value ( <=0 to quit ):\n"); scanf ("%d", & sec ); } printf (" Done!\n"); return 0; 30
% Convert secs to mins and secs! Enter the number of secs ( <=0 to quit ): 154 154 secs is 2 mins, 34 secs. Enter next value ( <=0 to quit ): 567 567 secs is 9 mins, 27 secs. Enter next value ( <=0 to quit ): 0 Done! 31
i // mod_negative.c: # include <stdio.h> int main ( void ) { printf (" 11 / 5 = %2d, 11 % 5 = %2d\n", 11 / 5, 11 % 5); printf (" 11 / -5 = %2d, 11 % -5 = %2d\n", 11 / ( -5), 11 % ( -5)); printf (" -11 / -5 = %2d, -11 % -5 = %2d\n", ( -11) / ( -5), ( -11) % ( -5)); printf (" -11 / 5 = %2d, -11 % 5 = %2d\n", ( -11) / 5, ( -11) % 5); return 0; } 32
11 / 5 = 2, 11 % 5 = 1 11 / -5 = -2, 11 % -5 = 1-11 / -5 = 2, -11 % -5 = -1-11 / 5 = -2, -11 % 5 = -1 33
11 / 5 = 2, 11 % 5 = 1 11 / -5 = -2, 11 % -5 = 1-11 / -5 = 2, -11 % -5 = -1-11 / 5 = -2, -11 % 5 = -1 C99 33
++ (increment operator) 1 ++i; i ++; 1 34
# include <stdio.h> int main ( void ) { int i = 0, j = 0; while (i < 5) { i ++; ++j; printf ("i = %d, j = %d\n", i, j); } return 0; } i = 1, j = 1 i = 2, j = 2 i = 3, j = 3 i = 4, j = 4 i = 5, j = 5 35
++i; j ++; i = i + 1; j = j + 1; 36
++i; j ++; i = i + 1; j = j + 1; 36
shoe = 8. 0; while ( shoe < 18. 5) { foot = SCALE * size + ADJUST ; printf (" %10.1 f %20.2 f inches \n", shoe, foot ); ++ shoe ; } 37
shoe = 7. 0; while (++ shoe < 18. 5) { foot = SCALE * size + ADJUST ; printf (" %10.1 f %20.2 f inches \n", shoe, foot ); } 38
// post_pre.c # include <stdio.h> int main ( void ) { int a = 1, b = 1; int aplus, bplus ; aplus = a ++; bplus = ++b; printf ("a = %d, aplus = %d\n", a, aplus ); printf ("b = %d, bplus = %d\n", b, bplus ); return 0; } 39
// post_pre.c # include <stdio.h> int main ( void ) { int a = 1, b = 1; int aplus, bplus ; aplus = a ++; bplus = ++b; printf ("a = %d, aplus = %d\n", a, aplus ); printf ("b = %d, bplus = %d\n", b, bplus ); return 0; } a = 2, aplus = 1 b = 2, bplus = 2 39
shoe = 7. 0; while (++ shoe < 18. 5) { foot = SCALE * size + ADJUST ; printf (" %10.1 f %20.2 f inches \n", shoe, foot ); } shoe = 7. 0; while ( shoe ++ < 18. 5) { foot = SCALE * size + ADJUST ; printf (" %10.1 f %20.2 f inches \n", shoe, foot ); } 40
b = ++i; ++i; b = i; 41
i = 5; b = ++i; i = 5; b = i ++; b i 42
-- count ; // count - -; // 43
x * y++ x * (y ++) (x * y)++ // invalid 44
45
// inc.c: # include <stdio.h> int main ( void ) { int y = 2; int n = 3; int nextnum ; nextnum = (y + n ++) *6; printf ("n = %d, nextnum = %d\n", n, nextnum ); return 0; } 46
// inc.c: # include <stdio.h> int main ( void ) { int y = 2; int n = 3; int nextnum ; nextnum = (y + n ++) *6; printf ("n = %d, nextnum = %d\n", n, nextnum ); return 0; } n = 4, nextnum = 30 46
n++ n n ++ n n n 47
// inc1.c: # include <stdio.h> int main ( void ) { int y = 2; int n = 3; int nextnum ; nextnum = (y + ++n) *6; printf ("n = %d, nextnum = %d\n", n, nextnum ); return 0; } 48
// inc1.c: # include <stdio.h> int main ( void ) { int y = 2; int n = 3; int nextnum ; nextnum = (y + ++n) *6; printf ("n = %d, nextnum = %d\n", n, nextnum ); return 0; } n = 4, nextnum = 36 48
n++ n ++n n 49
Don t Be Too Clever // inc2.c: # include <stdio.h> int main ( void ) { int n = 5; printf ("n = %d, n^2 = %d\n", n, n*n ++) ; return 0; } 50
Don t Be Too Clever // inc2.c: # include <stdio.h> int main ( void ) { int n = 5; printf ("n = %d, n^2 = %d\n", n, n*n ++) ; return 0; } maybe : n = 5, n ^2 = 25 maybe : n = 6, n ^2 = 25 maybe : n = 6, n ^2 = 30 50
Don t Be Too Clever // inc2.c: # include <stdio.h> int main ( void ) { int n = 5; printf ("n = %d, n^2 = %d\n", n, n*n ++) ; return 0; } maybe : n = 5, n ^2 = 25 maybe : n = 6, n ^2 = 25 maybe : n = 6, n ^2 = 30 C 50
Don t Be Too Clever // inc3.c: # include <stdio.h> int main ( void ) { int n = 5; int m; m = n/2 + 5*(1 + n ++) ; printf ("n = %d, m = %d\n", n, m); return 0; } maybe : n = 6, m = 32 maybe : n = 6, m = 33 51
Don t Be Too Clever // inc3.c: # include <stdio.h> int main ( void ) { int n = 5; int m; m = n/2 + 5*(1 + n ++) ; printf ("n = %d, m = %d\n", n, m); return 0; } maybe : n = 6, m = 32 maybe : n = 6, m = 33 51
Don t Be Too Clever // inc4.c: # include <stdio.h> int main ( void ) { int n = 3; int m; m = n++ + n ++; printf ("n = %d, m = %d\n", n, m); return 0; } 52
Don t Be Too Clever // inc4.c: # include <stdio.h> int main ( void ) { int n = 3; int m; m = n++ + n ++; printf ("n = %d, m = %d\n", n, m); return 0; } maybe : n = 5, m = 7 maybe : n = 5, m = 6 52
Don t Be Too Clever n 5 m m n n m 6 n 5 m n n n n m 7 n 5 53
54
C C++
C C++ C C++, 1. 55
C C++ C C++, 1. int i = (5, 10) ; // i = 10 int j = (f1 (), f2 ()); // j = f2 () 55
C C++ C C++, 1. int i = (5, 10) ; // i = 10 int j = (f1 (), f2 ()); // j = f2 () 2. enum 55
C C++ C C++, 1. int i = (5, 10) ; // i = 10 int j = (f1 (), f2 ()); // j = f2 () 2. enum int a = 1, b = 2; void fun (x, y); 55
C C++ void fun (f1 (), f2 ()); f1() f2() 56
C C++ // comma1.c: # include < stdio.h> int main () { int x = 10; int y = 15; printf ("%d\n", (x, y)); return 0; } 57
C C++ // comma1.c: # include < stdio.h> int main () { int x = 10; int y = 15; printf ("%d\n", (x, y)); return 0; } 15 57
C C++ // comma2.c: # include < stdio.h> int main () { int x = 10; int y = (x++, ++x); printf ("%d\n", y); return 0; } 58
C C++ // comma2.c: # include < stdio.h> int main () { int x = 10; int y = (x++, ++x); printf ("%d\n", y); return 0; } 12 58
C C++ // comma3.c: # include < stdio.h> int main () { int x = 10, y; y = (x++, printf ("x = %d\n", x), ++x, printf ("x = %d\n", x), x ++) ; printf ("y = %d\n", y); printf ("x = %d\n", x); return 0; } 59
C C++ // comma3.c: # include < stdio.h> int main () { int x = 10, y; y = (x++, printf ("x = %d\n", x), ++x, printf ("x = %d\n", x), x ++) ; printf ("y = %d\n", y); printf ("x = %d\n", x); return 0; } x = 11 x = 12 y = 12 x = 13 59
1. == true false 5 == 5 true 2.!= true false 5!= 5 false 60
3. > true false 6 > 5 true 4. < true false 6 < 5 false 61
5. >= true false 5 >= 5 true 6. <= true false 5 <= 5 true 62
i 1 // C program to demonstrate working of relational operators 2 # include <stdio.h> 3 int main () 4 { 5 int a=10, b =4; 6 // relational operators 7 // greater than example 8 if (a > b) 9 printf ("a is greater than b\n"); 10 else 11 printf ("a is less than or equal to b\n"); 12 // greater than equal to 13 if (a >= b) 63
ii 14 printf ("a is greater than or equal to b\n"); 15 else 16 printf ("a is lesser than b\n"); 17 // less than example 18 if (a < b) 19 printf ("a is less than b\n"); 20 else 21 printf ("a is greater than or equal to b\n"); 22 // lesser than equal to 23 if (a <= b) 24 printf ("a is lesser than or equal to b\n"); 25 else 26 printf ("a is greater than b\n"); 27 // equal to 28 if (a == b) 64
iii 29 printf ("a is equal to b\n"); 30 else 31 printf ("a and b are not equal \n"); 32 // not equal to 33 if (a!= b) 34 printf ("a is not equal to b\n"); 35 else 36 printf ("a is equal b\n"); 37 return 0; 38 } 65
Output : a is greater than b a is greater than or equal to b a is greater than or equal to b a is greater than b a and b are not equal a is not equal to b 66
1. && true false a b true ( ) a && b true 2. true false a b true ( ) a b true a b true a b true 3.! true false a false a true 67
i 1 // C program to demonstrate working of logical operators 2 # include <stdio.h> 3 int main () 4 { 5 int a = 10, b = 4, c = 10, d = 20; 6 // logical operators 7 // logical AND example 8 if (a>b && c==d) 9 printf (" a is greater than b AND c is equal to d\n"); 10 else 11 printf (" AND condition not satisfied \n"); 12 // logical OR example 68
ii 13 if (a>b c==d) 14 printf (" a is greater than b OR c is equal to d\n"); 15 else 16 printf (" Neither a is greater than b nor c is equal to d\n"); 17 // logical NOT example 18 if (!a) 19 printf ("a is zero \n"); 20 else 21 printf ("a is not zero "); 22 return 0; 23 } 69
AND condition not satisfied a is greater than b OR c is equal to d a is not zero 70
i false hello 1 # include <stdio.h> 2 # include < stdbool.h> 3 int main () 4 { 5 int a=10, b =4; 6 bool res = ((a == b) && printf (" Hello ")); 7 return 0; 8 } 71
hello 1 # include <stdio.h> 2 # include < stdbool.h> 3 int main () 4 { 5 int a=10, b =4; 6 bool res = ((a!= b) && printf (" Hello ")); 7 return 0; 8 } 72
true hello 1 # include <stdio.h> 2 # include < stdbool.h> 3 int main () 4 { 5 int a=10, b =4; 6 bool res = ((a!= b) printf (" Hello ")); 7 return 0; 8 } 73
hello 1 # include <stdio.h> 2 # include < stdbool.h> 3 int main () 4 { 5 int a=10, b =4; 6 bool res = ((a == b) printf (" Hello ")); 7 return 0; 8 } 74
4-6 4 + 21 a * (b + c/d) / 20 q = 5 * 2 x = ++ q % 3 q > 3 75
(subexpression) 76
-4 + 6 2 c = 3 + 8 11 5 > 3 1 6 + (c = 3 + 8) 17 C 77
(statement) (program) C 78
i = 4 i = 4; 79
C C 4; 3 + 4; 80
x = 25; ++x; y = sqrt (x); 81
x = 6 + (y = 5); y = 5 82
i // addemup.c: # include <stdio.h> int main ( void ) { int count, sum ; count = 0; sum = 0; while ( count ++ < 20) sum = sum + count ; printf (" sum = %d\n", sum ); return 0; } 83
(side effect) i = 50; i 50 84
(side effect) i = 50; i 50 Why? 84
(side effect) i = 50; i 50 Why? C C 4+6 C 10 C i = 50 C 50 i 50 84
(sequence point) 85
(sequence point) (full expression) while 86
while (i++ < 10) printf ("%d\n", i); i++ < 10 while C printf i 10 87
y = (4 + x ++) + (6 + x ++) ; 4 + x++ C x C x C x 88
(compound statement) (block) 89
index = 0; while ( index ++ < 10) sam = 10 * index + 2; printf (" sam = %d\n", sam ); index = 0; while ( index ++ < 10) { sam = 10 * index + 2; printf (" sam = %d\n", sam ); } 90
10 + 20 * 30 10 + (20 * 30) * / 100 / 10 * 10 (100 / 10)* 10 91
1 92
i // Associativity is not used in the below program. Output // is compiler dependent. int x = 0; int f1 () { x = 5; return x; } int f2 () { x = 10; return x; } 93
ii int main () { int p = f1 () + f2 (); printf ("%d ", x); return 0; } 94
+ f1() f2() 95
2 + - 96
3 ++ ++ ++ ++ ++ ++ 97
4 # include < stdio.h> int main () { int a; a = 1, 2, 3; // Evaluated as (a = 1), 2, 3 printf ("%d", a); return 0; } 98
5 c > b > a C (c > b)> a Python a < b and b < c # include <stdio.h> int main () { int a = 10, b = 20, c = 30; if (c > b > a) printf (" TRUE "); else printf (" FALSE "); return 0; } 99
1: () ( ) [] ( ). -> ++ -- / 100
2: ++ -- / + - /! ~ / (type) * & sizeof 101
3: * / % / / + - / << >> / < <= / > >= / ==!= / 102
4: & ^ &&?: 103
5: = += -= / *= /= / %= &= / ^= = / <<= >>= /, ( ) 104