6
    #include <stdio.h>

    int main()
    {
        int a = 10;
        if (a == a--)
            printf("TRUE 1\t");
        a = 10;

        if (a == --a)
            printf("TRUE 2\t");
    }

Why is the second if statement true?

Output is: TRUE 1 TRUE 2

Is this happening due to Undefined behavior because I am comparing same variable with its decremented value?

Stephan Burlot
  • 5,077
  • 3
  • 25
  • 26
Pankaj Mahato
  • 1,051
  • 5
  • 14
  • 26
  • Valid question. On the other hand: don't write code that is not obviously correct. Compressing code into as few lines as possible is a non-goal. – usr Jan 25 '14 at 12:22
  • 2
    Rather than post to ask this, I think you would learn more if you were to compile this to assembly and examine the output. (You can probably examine the assembly output from a normal compilation if you run it in your debugger too.) – mah Jan 25 '14 at 12:23
  • 1
    horribly wrong (incorrect!) title. – Karoly Horvath Jan 25 '14 at 12:23

3 Answers3

12

Correct, the condition evaluates to true because you see undefined behavior: if a variable with an operator that has side effects is used in an expression, it is illegal to use the same variable again in an expression that has no sequence points (== does not have sequence points).

This is because the compiler is free to apply the side effect of -- at any time that it wishes, as long as the value that is used when evaluating the expression is correct (i.e. the value before the decrement for postfix notation, or the value after the decrement for the prefix notation).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    And [GCC 4.8](http://gcc.gnu.org/gcc-4.8/) is warning you twice with `gcc -Wall`.... and so does CLANG 3.4 – Basile Starynkevitch Jan 25 '14 at 12:24
  • 2
    *it is illegal to use the same variable more then once in a single expression that has no sequence points* This is not true. You can use a variable more than once in an expression with no sequence points. The problem with the code is that the prior value is not being used only to determine the new value to be stored. `a + a + a + a` is valid; your answer states that it would be invalid. – Filipe Gonçalves Jan 25 '14 at 12:30
  • http://c-faq.com/expr/seqpoints.html – Amir Naghizadeh Jan 25 '14 at 12:34
  • @FilipeGonçalves Ah, of course - I should have mentioned an operator with side effects. Nice catch - thank you very much! – Sergey Kalinichenko Jan 25 '14 at 12:35
-3

a-- decrements a after the expression has evaluated, effectively meaning when a == a is calculated, it's 10 == 10, which is true.

--a decrements a before the expression has evaluated, effectively meaning when a == a is actually calculated, a has already been decremented, so it's 9 == 9, which is again true.

Pre and post decrements can't happen in the middle of expressions. They happen before or after them.

Clinton
  • 22,361
  • 15
  • 67
  • 163
-3

This is because associativity of == operator is left to right.

a==a--

In this expression, first both sides will be compared (10==10), and then a is decreased to 9

a==--a 

in this expression first the value of a is decreased to 9 then compared to a (which has become 9)

So, both are true.

Gaurav
  • 5
  • 6
  • 1
    No, this code invokes undefined behavior due to the lack of sequence points. The associativity tells you what an expression means, not what is the order of evaluation. – Filipe Gonçalves Jan 25 '14 at 12:34