360 likes | 436 Views
Time to Put on your thinking Hats. What would be the output. void main() { int a=32767; printf(“%d”, a); }. 32767. What would be the output. void main() { int a=32769; printf(“%d”, a); }. -32767. What would be the output. void main() { int a=1232.5; printf(“%d”, a); }.
E N D
What would be the output void main() { int a=32767; printf(“%d”, a); } 32767
What would be the output void main() { int a=32769; printf(“%d”, a); } -32767
What would be the output void main() { int a=1232.5; printf(“%d”, a); } 1233 1232
What would be the output void main() { int a=-32779.205; printf(“%d”, a); } 32757 Subtract 11-1=10 to 32767
What would be the output void main() { float a=-3279.205; printf(“%d”, a); } -3279
What would be the output void main() { int a=-3279.205; printf(“%f”, a); } -3279.000000
What would be the output void main() { float a=-32779.205; printf(“%f”, a); } -32779.205000
What would be the output void main() { float a=-32779.205; printf(“%.1f”, a); } -32779.2
What would be the output void main() { float a=69; printf(“%f”, a); } 69.000000
What would be the output and how many bytes will it occupy void main() { int a,b,c; a=5; b=2; c=a/b; printf(“%d”, c); } 2 & 6 bytes Size of int*3
What would be the output and how many bytes will it occupy void main() { inta,b; float c; a=5; b=2; c=a/b; printf(“%f”, c); } 2.000000 & 8 bytes Size of int*2 +Size of float
What would be the output and how many bytes will it occupy void main() { float a,b,c; a=5; b=2; c=a/b; printf(“%f”, c); } 2.500000 & 12 bytes Size of float*3
What would be the output and how many bytes will it occupy void main() { inta,b; float c; a=5; b=2; c=(float)a/b; printf(“%f”, c); } 2.500000 &8 bytes Size of int*2 +Size of float
A-Z 65-90 • a-z 97-122 • 0-9 48-57 • Special symbols: • 0-47 • 58-64 • 91-96 • 123-254
What would be the output void main() { char s=65; char ch=‘A’; char st=‘25’; printf(“%d”,ch); printf(“%c”,ch); printf(“%d”,s); printf(“%c”,s); printf(“%c”,st); printf(“%d”,st); } 65 A 65 A 2 50
£ • Try some of these: printf(“%c”,-100); printf(“%c”,-128); printf(“%c”,-130); printf(“%c”,100); printf(“%d”,-10); printf(“%x”,1>>4); printf(“%x”,16); Ç ~ d -10 ffff 10
Try some of these: printf(“%d”,-100); printf(“%.2f”,128); printf(“%f”,-130); printf(“%c”,91); printf(“%d”,34342); printf(“%x”,1004); printf(“%x”,16);
Associativity and precedence 9 #include<stdio.h> void main() { printf("%d",5+3*6/2-5); } 5+18/2-5 // division has higher precedence then + and - 5+9-5 //+ , - has same precedence so check associativity ie. L to r 9
Write a program to swap 2 numbers void main() { inta,b,c; a=5; b=2; c=a; a=b; b=c; printf(“%d,%d”,a,b); }
Suppose we print c over here what would be its value? • 5, as it copy paste the value and does not cut paste the values
Write a program to swap 2 numbers without using a third variable void main() { inta,b; a=5; b=2; a=a+b; b=a-b; a=a-b; printf(“%d,%d”,a,b); }
Similarly , can we Write a program to swap 2 numbers, using multiplication and division void main() { inta,b; a=5; b=2; a=a*b; ?=a/b; ?=a/b; printf(\n“%d,%d”,a,b); }
Similarly , can we Write a program to swap 2 numbers, using XOR(^) void main() { inta,b; a=5; b=2; a=a^b; b=a^b; a=a^b; printf(“\n%d,%d”,a,b); }
Logic behind XOR • a=5 101 • b=6 110 • a^b=3 011 • a=3 011 • b=6 110 • a^b=5 101 • a=3 011 • b=5 101 • a^b=6 110
Similarly , can we Write a program to swap 2 numbers in one line b = (a*a)/a + (a=b) - a; a^=b^=a^=b;
++ is increment by 1a++ is post increment++a is pre incrementa=a+1;-- is decrement by 1a-- is post decrement--a is pre decrementa=a-1;
What would be the output void main() { char s=5; s++; printf(“%d”,s); printf(“%d”,s++); printf(“%d”,s); printf(“%d”,++s); printf(“%d”,s); } 6 6 7 8 8
What would be the output void main() { int s=5; s++; printf(“%d”,s); printf(“%d”,s--); printf(“%d”,s); printf(“%d”,--s); printf(“%d”,s); } 6 6 5 4 4
What would be the output void main() { char s=5; printf(“%d”,s+++s); } What would the compiler read it as? a++ +a Or a+ ++a 5+6= 11 Note: Post increment has higher precedence than pre increment
Please try Some of these • printf(“%d”, s++s); • printf(“%d”, s++++s); • printf(“%d”, s+++++s); • printf(“%d”, s++ + ++s); • printf(“%d”,++s+++s); • printf(“%d”,++s+++s++); • printf(“%d”, s+s++); • printf(“%d”, s+s++++);
What would be the output void main() { char s=5; printf(“%d%d%d%d”,++s,++s,s++,++s); printf(“\n%d”,s); } 9 8 6 6 9 Note: Post increment has higher precedence than pre increment, but here associativety is Right to Left
The distance between 2 cities (in km) is input through the keyboard. Write a program to convert and print the distance in meters, feet, inches and centimeters. • 1 km=1000m • 1m=100cm • I inch=2.54 cm • 1 feet=12inch
What would be the output Bitwise Operators 3^2&~1 11 ^ 10 & ~ 01 11 ^ 10 & 10 11 ^ 10 01 1