-9

Was just posed an elementary programming question that I've somehow overlooked.

int a = 2, b = 3, c = 5;

if (!a == b)
    c = a--;
else
    c = ++b;

printf("%d %d %d\n", a, b, c);

I think the output should be 2 4 4. Anyone can help verify whether I'm correct or wrong and why?

4

2 回答 2

1

You are correct: !a giver you 0 and 0 == b is 0,

So, c = ++b gives b = 4 and c = 4. The a is not changed.

于 2013-06-23T15:57:03.697 回答
0

Since ! has higher precedence than == the condition of the if will be false ((!a) == b) -> (0 == 3). So the else will be executed, and b and c will both be set to 4. You were correct.

于 2013-06-23T15:54:31.367 回答