Why the expression a==--a true in if statement? [duplicate]
Asked Answered
M

3

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?

Monostome answered 25/1, 2014 at 12:19 Comment(3)
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.Hondo
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.)Pentobarbital
horribly wrong (incorrect!) title.Bidget
E
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).

Emcee answered 25/1, 2014 at 12:23 Comment(4)
And GCC 4.8 is warning you twice with gcc -Wall.... and so does CLANG 3.4Lubricous
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.Admiration
c-faq.com/expr/seqpoints.htmlLeftover
@FilipeGonçalves Ah, of course - I should have mentioned an operator with side effects. Nice catch - thank you very much!Emcee
T
-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.

Tamayo answered 25/1, 2014 at 12:28 Comment(1)
c&p error: you only do post decrementationCoprolalia
I
-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.

Interfuse answered 25/1, 2014 at 12:33 Comment(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.Admiration

© 2022 - 2024 — McMap. All rights reserved.