Comma Operator, C program Output Qn


#1

The output of the following program

int main ( )
{
int a=1,b=2,c=3;
printf (“%d”, a+=(a+=3, 5,a));
return 0;
}
will be

(i) 12
(ii) 6
(iii) 9
(iv) 8

with explanation please…


#2

Answer is option (iv) 8.

First the inner parenthesis would be evaluated. i.e.(a+=3,5,a)
It would be evaluated from left to right.
(a=a+3,5,a)
(a=1+3,5,a)
(a=4,5,a)
(a=4,5,a)

Now the outer statement would be evaluated and only the right most element would be assigned.
i.e.
a+=(a=4,5,a)
a+=a
a=a+a
a=4+4
a=8

References: