1 / 36

Time to Put on your thinking Hats

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); }.

malo
Download Presentation

Time to Put on your thinking Hats

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Time toPut on your thinking Hats

  2. What would be the output void main() { int a=32767; printf(“%d”, a); } 32767

  3. What would be the output void main() { int a=32769; printf(“%d”, a); } -32767

  4. What would be the output void main() { int a=1232.5; printf(“%d”, a); } 1233 1232

  5. What would be the output void main() { int a=-32779.205; printf(“%d”, a); } 32757 Subtract 11-1=10 to 32767

  6. What would be the output void main() { float a=-3279.205; printf(“%d”, a); } -3279

  7. What would be the output void main() { int a=-3279.205; printf(“%f”, a); } -3279.000000

  8. What would be the output void main() { float a=-32779.205; printf(“%f”, a); } -32779.205000

  9. What would be the output void main() { float a=-32779.205; printf(“%.1f”, a); } -32779.2

  10. What would be the output void main() { float a=69; printf(“%f”, a); } 69.000000

  11. 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

  12. 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

  13. 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

  14. 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

  15. A-Z 65-90 • a-z 97-122 • 0-9 48-57 • Special symbols: • 0-47 • 58-64 • 91-96 • 123-254

  16. 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

  17. £ • 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

  18. 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);

  19. 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

  20. 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); }

  21. 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

  22. 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); }

  23. 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); }

  24. 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); }

  25. 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

  26. Similarly , can we Write a program to swap 2 numbers in one line b = (a*a)/a + (a=b) - a; a^=b^=a^=b;

  27. Till now any problem?

  28. ++ 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;

  29. 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

  30. 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

  31. 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

  32. 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++++);

  33. 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

  34. 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

  35. What would be the output Bitwise Operators 3^2&~1 11 ^ 10 & ~ 01 11 ^ 10 & 10 11 ^ 10 01 1

  36. Best of Luck

More Related