Printf MCQ 6 : Constant Number Instead of Variable in Printf



Printf Multiple Choice Questions : Constant Number Instead of Variable in Printf


Predict the Output of the Following Program if Given Things are -

  • Two Variables are declared .
  • Two Variables are initialized
#include<stdio.h>
void main()
{
int input1 = 10,input2 = 20;
printf("%d %d",100);
}

Options :

  1. 100 20
  2. Garbage 20
  3. 20 10
  4. Garbage 0

[toggle title=”Output”]100 20[/toggle]


Switch to Printf MCQ Home : Click Here


How and Why ?

  1. Undefined Behavior of Printf
  2. Click Here to See All Observation of Undefined Behavior
  3. Here First %d <—> 100
  4. Second %d <—> Latest Initialized Variable

What will Happen If Declaration Line is Replaced by This Line ?

int input1 = 20,input2;

Output :

100 20

Why ?

  1. As Explained Above Here First %d <—> 100
  2. But Second %d Corresponds to Latest Initialized Variable which is input1 = 20