Reputation: 145
I have a question, how the compiler operate on the following code:
#include<stdio.h>
int main(void)
{
int b=12, c=11;
int d = (b == c++) ? (c+1) : (c-1);
printf("d = %i\n", d);
}
I am not sure why the result is d = 11
.
Upvotes: 11
Views: 899
Reputation: 1263
Translated to a regular if-statement your code would look like this:
int b=12, c=11;
int d;
if (b == c++)
d = c+1;
else
d = c-1;
The clue here is that c is incremented after the condition is checked. So you enter the else
state but c already has the value 12 there.
Upvotes: 4
Reputation: 4288
Beacuse the condition is false, therefore the false
case will happen: c-1
, but since you incremented c
in the condition by c++
, therefore c
is now 12
. The result thus 12 - 1 which is 11.
EDIT: What OP misunderstood was the post increment.
So what actually happen is like this:
#include<stdio.h>
int main(void)
{
int b=12, c=11;
int d;
if (b == c) { // 12 == 11 ? -> false
c = c + 1;
d = c + 1;
} else { // this executes since condition is false
c = c + 1; // post increment -> c++ -> c = 12 now
d = c - 1; // 12 - 1 = 11 -> d = 11
}
printf("d = %i\n", d);
}
Upvotes: 4
Reputation: 222457
In int d = (b == c++) ? (c+1) : (c-1);
:
c++
is the current value of c
, 11. Separately, c
is incremented to 12.b == 11
is false, since b
is 12.(b == c++)
is false, (c-1)
is used. Also, the increment of c
to 12 must be completed by this point.c
is 12, c-1
is 11.d
is initialized to that value, 11.Upvotes: 6
Reputation: 310950
According to the C Standard (6.5.15 Conditional operator)
4 The first operand is evaluated; there is a sequence point between its evaluation and the evaluation of the second or third operand (whichever is evaluated). The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0; the result is the value of the second or third operand (whichever is evaluated), converted to the type described below.110)
So in the initializing expression of this declaration
int d = (b == c++) ? (c+1) : (c-1);
the variable b
is compared with the value of the variable c
because the post-increment operator returns the value of its operand before incrementing it.
As the values are not equal each other (b
is set to 12 while c
is set to 11) then the sub-expression (c-1)
is evaluated.
According to the quote there is a sequence point after evaluation of the condition of the operator. It means that after evaluation of the condition c
has the value 12
after applying the post-increment operator to the variable c
. As a result the variable d is initialized by the value 1
(12 - 1
).
Upvotes: 5
Reputation: 252
Refer to Ternary Operator.
Syntax
condition ? value_if_true : value_if_false
So, you wrote
int d = (b == c++) ? (c+1) : (c-1);
In this situation, the result will be 11 because, after if checks, 'c' value is increased(c+1=12) and only after that it sets 'd' value as c(12)-1 which is 11.
If you used, for example:
int d = (b == ++c) ? (c+1) : (c-1);
"c" value would be increased before checking the statement, so it would be true and "d" value would be c(12)+1 which is 13.
Upvotes: 1