3
    #include <stdio.h>

    int main()
    {
        int x = 0;

        if (x++)
            printf("true\n");
        else if (x == 1)
            printf("false\n");
        return 0;
    }

Output:

false

Why is the output false?

x++ is post increment; this means that the value of x is used then it is incremented. If it is so, then x=0 should be used and the answer should be true.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Pankaj Mahato
  • 1,051
  • 5
  • 14
  • 26
  • 2
    Because try `++x` and you'll know. – Maroun Jan 29 '14 at 15:13
  • At one time, this was marked as a duplicate of [Post-increment operator behaviour](http://stackoverflow.com/questions/98242/post-increment-operator-behavior) but (as I noted in April 2014 in a comment I've since removed), the content of the duplicate is about undefined behaviour from using both `i` and `++i` in two arguments to a function call — which is not a good match or this question, therefore (even though the title is a good match). This question has no undefined behaviour. – Jonathan Leffler Jan 01 '15 at 03:11

6 Answers6

10

In C, 0 is treated as false. In x++, the value of x, i.e, 0 is used in the expression and it becomes

if(0)  // It is false
    printf("true\n");  

The body of if doesn't get executed. After that x is now 1. Now the condition in else if, i.e, x == 1 is checked. since x is 1 , this condition evaluates to true and hence its body gets executed and prints "false".

haccks
  • 104,019
  • 25
  • 176
  • 264
4

Post increment means that it returns the current value (in this case for the purpose of the if) and increments it afterwards. It is equivalent to

if(x) {
  x++;
  // ...
} else {
  x++;
  // ...
}
Sergey L.
  • 21,822
  • 5
  • 49
  • 75
2

0 is equivalent to false in C. As you are using post-increment operator, condition is evaluated before increment so x is false and printf("true\n"); is never executed. Then goes to else and succeeds evaluating x == 1, then prints false.

As a good practice, try to avoid assignations in condition sentences.

Javier
  • 801
  • 3
  • 10
  • 24
1

0 is false in C. You're using the post-increment operator.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
1

You yourself wrote: "x++ is post increment, this means that the value of x is used then it is incremented"

Consider what that means:

  1. x is 0

  2. The expression is evaluated, 0 is false, so the expression is false.

  3. The post increment happens, changing x from 0 to 1.
    (After the expression was evaluated)

abelenky
  • 63,815
  • 23
  • 109
  • 159
1

My opinion is that a better response to the relation between 'if' statement and the post increment operator '++' requires the expansion of your C-code into Assembly. Trying to figure it out under the constricting logic of blocks of the "if ... else" statement, of high level languages, might be misleading because the flow control is read in different terms.

Consider that the pre and the post operators rely on the "change-then-use" and on the "use-then-change" rules respectively, where 'change' is meant by 'increment' and 'use' by the 'comparison'. So your input C-code basically turns into this raw pseudo-assembly:

; evaluating the first condition
mov x,0 // set x = 0
cmp x,0 // use (for comparison)
inc x // then change (now x is 1)
je print1
; evaluating the second condition
mov eax,1
cmp eax,x // evaluates to true
je print2

print1:
   printf("true\n");

print2:
  printf("false\n");

Take care that compilers might not put the inc instruction at the same position, that is, at the top or at the bottom of the labelled block of instructions. Hope it helps!

Sandro Rosa
  • 507
  • 4
  • 12