Hoang.T
Hoang.T

Reputation: 63

Basic programming exercise about logical operators

I have a problem with a question in my book:

#include<stdio.h>
void main()
{
    int a=5, b=-7, c=0, d;
    d = ++a && ++b || ++c;
    printf("\n%d%d%d%d",a,b,c,d);
}

The question asks me what is the output of the code. I ran it and the result on the screen is 6-601. I understand why a=6 and b=-6, but I don't understand why c=0 and d=1?

Upvotes: 6

Views: 1320

Answers (2)

Sourav Ghosh
Sourav Ghosh

Reputation: 134326

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.

Upvotes: 16

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31153

The || OR operator is short-circuiting, which means that if the left side is true then the right side is not evaluated. In this case ++a && ++b evaluates to true, so ++c is never run and c keeps its value of zero.

Also since it evaluates to true, this is denoted with 1 which is stored in d.

Any non-zero value is considered to be true and the result of boolean operations is defined to be 0 or 1 as an integer.

Upvotes: 14

Related Questions