Undefined Behavior of Printf Statement : printf MCQ
Undefined Behavior of Printf Statement :
- If Variable name is not specified in the printf then the behavior of printf statement is undefined in C Programming .
- Though its behavior is unknown , we have concluded some points on the basis of Observation .
- 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 :
- If no Variable is declared then it Prints 0
- If One Variable is declared and initialized then it prints 2 Values.
- First value is same as that of the value of Variable and 2nd as Zero
- 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 ).
- 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
- Latest Initialized Variable’s Value is Taken if variable is not specified in the printf
- If Two Variables and one %d is written then one which is initialized recently is Printed