Undefined Behavior of Printf Statement : printf MCQ



Undefined Behavior of Printf Statement :

  1. If Variable name is not specified in the printf then the behavior of printf statement is undefined in C Programming .
  2. Though its behavior is unknown , we have concluded some points on the basis of Observation .
  3. Note : These Observations are Not Standard .

Observation 1 : Don’t Declare any Variable

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

Output :

0

Observation 2 : Declare only 1 Variable

#include<stdio.h>
void main()
{
int a=10;
printf("%d %d");
}

Output :

10 0

Our Observation :

  1. If no Variable is declared then it Prints 0
  2. If One Variable is declared and initialized then it prints 2 Values.
  3. First value is same as that of the value of Variable and 2nd as Zero
  4. Instead if we declare 2 initialized Variable and we have 3 %d then it prints 3 values ( Value of Second Variable is printed first after that Value of 1st Variable is Printed then Zero is Printed ).
  5. Compiled And Run on : Borland CC++ 3.0

Observation 3 : Declare 2 Variables

#include<stdio.h>
void main()
{
int a=20,b=60;
printf("%d %d %d");
}

Output :

60 20 0

Special Notes

  1. Latest Initialized Variable’s Value is Taken if variable is not specified in the printf
  2. If Two Variables and one %d is written then one which is initialized recently is Printed