11

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.

unDeadHerbs
  • 1,306
  • 1
  • 11
  • 19
J0S
  • 145
  • 7

5 Answers5

6

In int d = (b == c++) ? (c+1) : (c-1);:

  • The value of c++ is the current value of c, 11. Separately, c is incremented to 12.
  • b == 11 is false, since b is 12.
  • Since (b == c++) is false, (c-1) is used. Also, the increment of c to 12 must be completed by this point.
  • Since c is 12, c-1 is 11.
  • d is initialized to that value, 11.
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
5

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).

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    The only correct answer - this specific case must be answered by mentioning the sequence point in `?:`. Because normally in C, combining `++` with other operations on the same operand is undefined behavior. And this code only works predictably because the `?:` has various special snowflake rules. – Lundin Feb 11 '20 at 13:04
4

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);
}
Eraklon
  • 4,206
  • 2
  • 13
  • 29
  • 1
    I think the OP is referring to the order of operations, given the `c++` in the condition. The condition is false, but then the *orginal* value of `c` is used to compute `c - 1`, not the incremented version. – chepner Feb 11 '20 at 12:45
  • But thats not true since the new c value is used or am I missing the point of yours? – Eraklon Feb 11 '20 at 12:46
  • I think there might be a misunderstanding between `c++` and `++c` – ChatterOne Feb 11 '20 at 12:46
  • @N00b `c++` is the *post*-increment operator. The value of `c++` is 11, with the side effect of making `c == 12`. `++c` would have the value of 12. – chepner Feb 11 '20 at 12:46
  • Actually what will happen is 12 == 11 condition check, then c is incremented. – Eraklon Feb 11 '20 at 12:47
4

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Odysseus
  • 1,213
  • 4
  • 12
1

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.

Neto Costa
  • 242
  • 2
  • 16