I believe you already got your answer, but just to elaborate a bit step-by-step, let me add one more clarification here. Firstly, to quote the properties of the &&
and ||
operators, from C11
standard, chapter §6.5.13 and §6.5.13, respectively,
(I)
The &&
operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it
yields 0. [...] If the first operand compares equal to 0, the second
operand is not evaluated.
and
(II)
The ||
operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it
yields 0. [...]. If the first operand compares unequal to 0, the second operand is
not evaluated.
and they both guarantee left-to-right evaluation. So, comparing your code,
d = ++a && ++b || ++c;
it happens like
d = ((++a && ++b) || ++c );
which evaluates to
d = (( 6 && ++b ) || ++c);
and then
d = ( ( 6 && (-6) ) || ++c);
Now in above stage, (I) is fulfilled and it comes down to
d = ( 1 || ++c);
Now, following the emphasis, which already meets the (II), so no further evaluation of the RHS operand of ||
is performed (i.e., ++c
is not evaluated), and it appears to be d = 1
and the final result, 1, is stored into d
.
That's how, a == 6
, b == -6
, c == 0
and d ==1
.
Having said that, void main()
should be changed to int main(void)
, at least to conform with the standard.