What will be the output of the following code? int main() {
What will be the output of the following code?
#include<stdio.h>
int main() {
int x=5,y;
y=x++ + ++x;
printf("%d",y);
return 0;
}
Show answerHide answer
Correct answer: (C) 12
Explanation
x++ gives 5 (x becomes 6), then ++x makes x 7, so y = 5 + 7 = 12 on common compilers. Strictly, modifying x twice in one expression is undefined behaviour in C.