Mukezh

Session 4 Printf

Dec 4th, 2020 (edited)
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. PRINTF
  2. 1. It will print 1st Argument on Screen (Except format Specifier %d , which will be Replaced )
  3. Syntax=> printf("arg1",arg2,arg3,.....,argn);
  4. 2. 1st Arg must be in pair of Double quotes ("").
  5. 3. If there is more than 1 arg, then those arg can be seperated by commas (,).
  6.  
  7. Example:
  8. printf("hello") Semicolon (;) Syntax error
  9. printf("hello"); hello
  10. printf(" hello "); (space)hello(space)
  11. printf("hello %d",10); hello 10
  12. printf("hello %d hello %d %d",10,20,30); hello 10 hello 20 30
  13. 1st Format Specifier (%d) => 2nd Arg (10)
  14. 2nd FS => 3rd Arg
  15. 3rd FS => 4th Arg
  16. Nth FS => (N+1)Arg
  17. printf("%d%d%d",10,20,30); 102030
  18. printf("%d %d %d",10,20,30); 10 20 30
  19. printf("%d %d %d",10,20); 10 20 GV
  20. printf("%d %d %d",10); 10 GV GV
  21. printf("%d %d %d"); GV GV GV
  22. Note: For a given format Specifier if there is no arg, then result is Garbage Value(GV)
  23. Garbage value can any value stored by compiler.
  24. printf("%d %d",10,20,30,40); 10 20
  25.  
  26. printf("%d",10+20); 30 here '+' act as Operator
  27. printf("10+20"); 10+20 here '+' is meant for printing
  28. printf("%d,%d",10,20); 10,20
  29. 1st Comma is for printing
  30. Next 2 Commas is for seperating arguments
  31.  
  32. printf("%d + %d",2+0); 2 + Gv
  33. printf("%d * %d = %d",5,2,5+2); 5 * 2 = 7
  34. * Meant for printing
  35. + Meant for Adding values
  36. printf("%f",3.6); 3.600000
  37. printf("%d",3.0); Gv
  38. !X!X!X!X!X! Format Specifier Cannot convert the data from one type to another type. It just print only the data specified.
  39. printf("%f",3); Gv
  40. printf("%d",100.6); Gv
  41. printf("%f",5/2); Gv
  42. printf("%d",3.0/2); Gv
  43. printf("%f",3.0/2); 1.5
  44. printf("%f",3.0%2); Error ----> % does not act on float value
  45. printf("%f",-5%2); Gv
  46. printf("Hello","hai","bye"); hello
  47. >>> Since there is NO FORMAT SPECIFIER in 1st Arg, it does not go to the second arg.
  48. Exercise
  49. 1. printf(" %d ",0 ); 0
  50. 2. printf(" %f",-3); Gv
  51. 3. printf(" %d ",3/4/1); 0
  52. 4. printf(" %f",3+5*1); GV
  53. 5. printf(" %f ", 12-5.0*2 ); 2.0000000
  54. 6. printf(" %i",-3.0); GV
  55. 7. printf(" %f %i %c %d ",0.1,10,0,11); 0.1 10 GV 11
  56. 8. printf("%d %d %d", "%d %d" ,10 ,20 ,30 ,40 ,50); GV 10 20
  57.  
  58. int amt=8500;
  59. 9. printf(amt); Error Absence of " "
  60. 10. printf(" %f ",amt-amt); GV
  61. 11. printf("amt"); amt
  62. 12. printf("%d",amt); 8500
  63. 13. printf("amt = %d"); amt = gv
  64. 14. printf("amt = %d",amt); amt = 8500
  65. 15. printf("%d+1000",amt); 8500+1000
  66. 16. printf("%d",amt+1000); 9500
  67. 17. printf(" amt+ 1000 " ); amt+1000
  68. 18. printf(" %d + %d = %d ",amt,amt,amt ); 8500 + 8500 = 8500
  69. 19. printf(" %d ",amt+500); 9000
  70. 20. printf(" amt = %d + %d",amt+100); amt = 8600 + GV
  71. 21. printf(" %d ",amt=amt+100); 8600
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
Add Comment
Please, Sign In to add comment