Printf MCQ 7 : printf(1+”%d”);



Printf Multiple Choice Questions  : printf(1+”%d”);


Question : Guess the output of Program

#include<stdio.h>
void main()
{
printf(1+"%d");
printf(1+"%c");
}

Options :

  1. Garbage
  2. dc
  3. cd
  4. %d%c

[toggle title=”Output”]dc[/toggle]


Switch to Printf MCQ Home :  Click Here


How and Why ?

  1. Printf Returns the Length of String specified in Double Quotes
  2. Consider first printf , Length of String specified in first printf i.e “%d” is 2
  3. It starts printing the String Skipping the number of Characters specified before ‘+’ sign
  4. printf(1+”%d”) skips “%” character and only prints ‘d’ as output.
  5. Similarly printf(1+”PRITESH”)  prints “RITESH” skipping first “P”.
  6. Similarly printf(2+”PRITESH”)  prints “ITESH” skipping 2 characters “PR”.

Note : 

  1. Operation is similar as Substring Operation
  2. While writing such Statement , Variable is not taken into Consideration .
  3. Confused ?
#include<stdio.h>
void main()
{
int var=10;
printf(1+"%d",var);
}
  1. Above statement also prints “d” as output. i.e Variable is Simply Neglected by Compiler.