Last Updated : 14 Feb, 2019

What will be the output of the following program?

#include <stdio.h>

int main() 
{
    int x = 10, y; 

    y = (x++, printf(\"%d \", x), ++x, printf(\"%d \", x), x++); 

    printf(\"%d \", y); 
    printf(\"%d \", x); 

    return 0; 
} 

(A) 11 11 12 12
(B) 10 11 12 12
(C) 11 12 12 13
(D) None of these


Answer: (C)

Explanation: The comma operator (represented by the token \’,\’) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type).
Hence y = (x++, printf(\”%d \”, x), ++x, printf(\”%d \”, x), x++);
will be evaluated as
x = 11
print x
x = 12
print x
y = x
x = 13

Therefore final value of x and y are 13 and 12 respectively.

Quiz of this Question


Share your thoughts in the comments