Discover the depth  of  C

  C C++  Linux ProgrammingNEW   Operating Systems  Data StructuresNEW  Compilers  Contact Us

Navigation

?? Interview Questions ??New

Join C4Swimmers

 sitepromotion.gif (577 bytes) New Member

Weekly Queries New

 Queries for this week

 All Weekly Queries

Code Zone - Free

Newbie Level

Intermediate Level

Tools Zone

C/C++ Tools

Test Your C/C++ Skills

  Playing with printf

  Playing with scanf

  Branching & Loops

  Pointers Part I

  Pointers Part II

  Structure & Union

C Special

  Discovering C
  IFAQs

  Self-reproducing C Code

  C/C++ Links
  C/C++ Books NEW

Programming Problems

  Part I
  Part II
Downloads
C4S - Solution Pack
Data Structures
C++ & 8086 ALP
ADA & System S/w
Online Certifications
premiumservices.gif (1070 bytes) Brainbench
premiumservices.gif (1070 bytes) Benchmarks Global
Help and Support
customer_care_small.gif (1018 bytes)  Suggestions
post-question_icon.gif (1062 bytes)  Contribute
premiumservices.gif (1070 bytes)  Advertise with us
memberhome.gif (1052 bytes)  -Home Page-

premiumservices.gif (1070 bytes) Feedback

As a webmaster of this site, I regard you as our most important critic and commentator. We value your opinion and want to know what we are doing right, what we could do better and any other words of wisdom you're willing to pass our way. It would be our greatest motivation which will help us in developing areas you would like to see in this site. We hope you will continue to encourage and support us in our future endeavors.

 
.  

C For Swimmers : Mastering C step-by-step

Google


WWW c4swimmers.esmartguy.com
NOTE : It is assumed that -

Necessary header files are included
Programs/Snippets are tested under Linux as well as Windows platform
Programs/Snippets are compiled using VC++ 6.0 as well as gcc 3.2
The underlying machine is an x86 system

Solutions - Playing with printf()

 [Q001] What will be the output of the following program :
 int main()
 {
   printf();
   return(0);
 }
 (a)Run-Time Error
 (b)Compile-Time Error
 (c)No Output
 (d)None of these
 Too few arguments to function 'printf'.
 [Q002] What will be the output of the following program :
 int main()
 {
   printf(NULL);
   return(0);
 }
 (a)Run-Time Error
 (b)Compile-Time Error
 (c)No Output
 (d)None of these
Here NULL is treated as a NULL string.
 [Q003] What will be the output of the following program :
 int main()
 {
   printf("%%",7);
   return(0);
 }
 (a)7
 (b)Compile-Time Error
 (c)%
 (d)%%
% is a format specifier and it prints %. The excess arguments are merely ignored i.e. 7.
 [Q004] What will be the output of the following program :
 int main()
 {
   printf("//",5);
   return(0);
 }
 (a)5
 (b)Compile-Time Error
 (c)/
 (d)//
\ is an escape sequence but not /. Thus it prints // and the excess arguments are merely ignored i.e. 5.
 [Q005] What will be the output of the following program :
 int main()
 {
   printf("d%",8);
   return(0);
 }
 (a)8
 (b)Compile-Time Error
 (c)d%
 (d)None of these
%d is a format specifier but not d%. Thus it prints d% and it ignores the excess arguments i.e. 8.
 [Q006] What will be the output of the following program :
 int main()
 {
   printf("%d"+0,123);
   return(0);
 }
 (a)123
 (b)Compile-Time Error
 (c)No Output
 (d)None of these
"%d"+0 in the printf statement has no effect on the output operation. Thus it prints 123.
 [Q007] What will be the output of the following program :
 int main()
 {
   printf("%d"+1,123);
   return(0);
 }
 (a)123
 (b)Compile-Time Error
 (c)d
 
(d)No Output
"%d"+1 (or > 0) in the printf statement affects the program output by considering "%d" as string and ignores the excess argument i.e. 123. Here 1 refers to the index i.e. 2nd character in the array or string "%d"
 [Q008] What will be the output of the following program :
 int main()
 {
   printf("%d",printf("Hi!")+printf("Bye"));
   return(0);
 }
 (a)ByeHi!6
 (b)Hi!Bye6
 (c)Compile-Time Error
 (d)None of these
L->R priority and the length of the strings 'Hi!' & 'Bye' is 3+3=6. The statement printf returns the no. of bytes displayed onto the screen.
 [Q009] What will be the output of the following program :
 int main()
 {
   printf("%d",printf("Hi!")*printf("Bye"));
   return(0);
 }
 (a)ByeHi!6
 (b)Hi!Bye9
 (c)Hi!Bye
 (d)None of these
L->R priority and the length of the strings 'Hi!' & 'Bye' is 3*3=9. The statement printf returns the no. of bytes displayed onto the screen.
 [Q010] What will be the output of the following program :
 int main()
 {
   printf("%d",printf("")+printf(""));
   return(0);
 }
 (a)0
 
(b)No Output
 (c)Compile-Time Error
 (d)None of these
L->R priority and the length of the two strings are 0+0=0. The statement printf returns the no. of bytes displayed onto the screen.
 [Q011] What will be the output of the following program :
 int main()
 {
   printf("Hi Friends"+3);
   return(0);
 }
 (a)Hi Friends
 (b)Friends
 (c)Hi Friends3
 (d)None of these
(base adress of the string 'Hi Friends')+0 points to the value 'H'. Now the NEW (base address) equals (base address)+3 that points to the character 'F'. Thus it prints the string from 'F' onwards.
 [Q012] What will be the output of the following program :
 int main()
 {
   printf("C For ") + printf("Swimmers");
   return(0);
 }
 (a)Compile-Time Error
 (b)C For Swimmers
 (c)Run-Time Error
 (d)None of these
The value returned by the printf statements need not be stored or simply ignored. It is a valid C code.
 [Q013] What will be the output of the following program :
 int main()
 {
   printf("\/\*\-*\/");
   return(0);
 }
 (a)Run-Time Error
 (b)\/*-*\/
 (c)/*-*/
 (d)None of these
\ is an escape sequence character. Be careful while analyzing such statements.
 [Q014] What will be the output of the following program :
 int main()
 {
   int main=7;
   {
      printf("%d",main);
      return main;
   }
   printf("Bye");
   return(0);
 }
 (a)Compile-Time Error
 (b)Run-Time Error
 (c)7Bye
 (d)7
It is a VALID C code. Prints 7 and returns the same to the Operating System. NOTE: Last printf statement will not be executed.
 [Q015] What will be the output of the following program :
 int main()
 {
   main();
   return(0);
 }
 (a)Compile-Time Error
 (b)Executes ONLY once
 (c)Infinite Loop
 (d)None of these
main is a function so it can be called by any other function including itself. Here main is called by itself and it runs infinite number of times i.e. till the memory exists.
 [Q016] What will be the output of the following program :
 int main()
 {
   printf("Work" "Hard");
   return(0);
 }
 (a)Work
 (b)Hard
 (c)No Output
 (d)WorkHard
It is a valid C code. First it prints the word 'Work' and then 'Hard'.
 [Q017] What will be the output of the following program :
 int main()
 {
   char str[]="%d";
   int val=25;
   printf(str,val);
   return(0);
 }
 (a)Compile-Time Error
 (b)Run-Time Error
 (c)25
 (d)None of these
First parameter contains the format specifier & the Second parameter contains the actual value 25.
 [Q018] What will be the output of the following program :
 int main()
 {
   int val=75;
   printf("%d",val,.,.);
   return(0);
 }
 (a)Compile-Time Error
 (b)No Output
 (c)75
 (d)None of these
Generates compile-time error message. Parse error before '.' token.
 [Q019] What will be the output of the following program :
 int main()
 {
   int val=10;
   printf("%d",val+1,"%d",val--);
   return(0);
 }
 (a)10
 (b)11 10
 (c)11 9
 (d)10 9
R->L priority. The second format specifier '%d' is an excess argument and it is ignored.
 [Q020] What will be the output of the following program :
 int main()
 {
   int val=5;
   printf("%d %d %d %d",val,--val,++val,val--);
   return(0);
 }
 (a)3 4 6 5
 (b)Compiler dependant
 (c)4 4 5 5
 (d)None of these
The output varies from compiler to compiler in different platforms.
 [Q021] What will be the output of the following program [NOTE : ASSUME 2 values are entered by the user are stored in the variables 'val' & 'num' respectively] :
 int main()
 {
   int val=5,num;
   printf("%d",scanf("%d %d",&val,&num));
   return(0);
 }
 (a)1
 (b)2
 (c)5
 (d)None of these
It is a valid C code. scanf statement is embedded inside the printf statement get executed first. scanf statement returns the number of input fields successfully scanned, converted and stored.
 [Q022] What will be the output of the following program :
 #define Compute(x,y,z) (x+y-z)
 int main()
 {
   int x=2,y=3,z=4;
   printf("%d",Compute(y,z,(-x+y)) * Compute(z,x,(-y+z)));
   return(0);
 }
 (a)40
 (b)30
 (c)Compile-Time Error
 (d)None of these
During pre-processing the macro calls are merely replaced by the replacement text. Be careful while analyzing macro calls.
 [Q023] What will be the output of the following program :
 int main()
 {
   int m=10,n=20;
   printf("%d %d %d",m/* m-value */,/* n-value */n,m*/* Compute m*n */n);
   return(0);
 }
 (a)Run-Time Error
 (b)10 20 200
 (c)Compile-Time Error
 (d)None of these
Comments /*...*/ are simply ignored by the compiler.
 [Q024] What will be the output of the following program :
 int main()
 {
   int m=10,n=20;
   /* printf("%d",m*n);
   return(0);
 }
 (a)No Output
 (b)Prints 200
 (c)Compile-Time Error
 (d)None of these
Unterminated comment or Comment statement not ended properly i.e */ is missing in the above C code.
 [Q025] What will be the output of the following program :
 int main()
 {
   int val=97;
   "Printing..."+printf("%c",val);
   return(0);
 }
 (a)Printing...97
 (b)97
 (c)Compile-Time Error
 (d)a
Alphabet 'a' is the ASCII equivalent of 97. printf statement return the number of bytes displayed onto the screen and it can be ignored.
 [Q026] What will be the output of the following program :
 int main()
 {
   int val=5;
   val=printf("C") + printf("Skills");
   printf("%d",val);
   return(0);
 }
 (a)Skills5
 (b)C1
 (c)Compile-Time Error
 (d)CSkills7
printf statement return the number of bytes displayed onto the screen. The length of the strings 'C' and 'Skills' are 1 + 6 = 7.
 [Q027] What will be the output of the following program :
 int main()
 {
   char str[]="Test";
   if ((printf("%s",str)) == 4)
      printf("Success");
   else
      printf("Failure");
   return(0);
 }
 (a)TestFailure
 (b)TestSuccess
 (c)Compile-Time Error
 (d)Test
It is valid C code. printf statement return the number of bytes displayed onto the screen.
 [Q028] What will be the output of the following program :
 int main()
 {
   int val=5;
   printf("%*d",val,val);
   return(0);
 }
 (a)bbbbb5 (where b means blankspace)
 (b)5
 (c)Compile-Time Error
 (d)None of these
Here '*' specifies the precision. First val is the precision and second val is the actual value. Thus it prints 5 BLANK SPACES & then the value 5.
 [Q029] What will be the output of the following program :
 int main()
 {
   int val=5;
   printf("%d5",val);
   return(0);
 }
 (a)Compile-Time Error
 (b)5
 (c)55
 (d)bbbbb5 (where b means blankspace)
%d format specifier is replaced by the contents of the variable val i.e. 5 followed by another 5.
 [Q030] What will be the output of the following program :
 int main()
 }
   int val=5;
   printf("%d",5+val++);
   return(0);
 {
 (a)Compile-Time Error
 (b)5
 (c)10
 (d)11
Incorrect usage of pair of braces } and {.  Correct usage : Each compound statement should be enclosed within a pair of braces, i.e. { and }.

You are Visitor No.

Sign my Guestbook View my Guestbook


Subscribe to C4Swimmers Group
Designed and Maintained by  Nanda Kishor

Make money from your website

Thanks for using C For Swimmers.
Regarding this material, you can send Bug Reports, Suggestions, Comments, etc. to

nandakishorkn@rediffmail.com

TheFreeSite.com!

Note: All logos or trademarks are related to their respective owners.

Although every precaution has been taken, the designer(s) owe no responsibility for malicious errors.

No liability assumed for damages resulting from the use of the available information.