What is the output/behavior of the following C program? #include<stdio.h> int…
2020
What is the output/behavior of the following C program?
#include<stdio.h>
int main()
{
int i = 5;
printf("%d %d %d", i++, i, ++i);
return 0;
}
- A.
7 6 6
- B.
6 7 8
- C.
Undefined behavior
- D.
5 6 7
Attempted by 1096 students.
Show answer & explanation
Correct answer: C
The program has undefined behavior. In the printf call, the variable i is modified twice, by i++ and ++i, and is also read as i in another argument. The C standard does not specify an evaluation order for function arguments, and these side effects are unsequenced relative to each other. Because of this, the program does not have one guaranteed output. Therefore, the correct conclusion is undefined behavior.