What is the output of the code written in C? void main()
What is the output of the code written in C?
void main()
{
int k, val = 1;
k = ++val * ++val;
printf("%d", k);
}
Show answerHide answer
Correct answer: (C) 9
Explanation
Modifying val twice in one expression is undefined behaviour in C, but common compilers (e.g. GCC) apply both increments first, making val = 3, and then compute 3 * 3 = 9.