-6
int a = 5;
if(a==a++){
   printf("true 1");
}
if(a==++a){
   printf("true 2");
}

When I run this code, it prints "true 2". I do not understand how. Please help. Also, how is logical equivalence computed in precedence with increment operators?

  • 4
    It will output a PIZZA with extra cheese. – haccks Jan 31 '16 at 13:58
  • This code yields random results because your variable "a" is initialized to random value each time you run it. – 4pie0 Jan 31 '16 at 13:58
  • I got double pepperoni with mine. – Martin James Jan 31 '16 at 13:59
  • 1
    UB in as many ways as possible. – Sourav Ghosh Jan 31 '16 at 14:00
  • 1
    Come on, this question is asked once every couple of days. Please read an introductory text on the `++` operators and search the archive before asking this question again. – fuz Jan 31 '16 at 14:11
  • I'm trying to understand how increment works with ==. I couldn't find it anywhere. I'm sorry if it's being repeated. – Asha Aravind Jan 31 '16 at 14:14
  • 2
    I don't understand why these questions come up in university/interview questions. The right answer is: "Stop writing programs like this! What are you doing!" – Richard Tingle Jan 31 '16 at 14:15
  • 1
    @AshaAravind not just repeated . but disgorged onto SO multple times by every generation of student. Please ask your prof/TA to not issue such homework in future. – Martin James Jan 31 '16 at 14:48
  • 1
    I mean, just your title alone :'C: What is the output of the following code?' - you already know the answer: ' When I run this code, it prints "true 2" ',so why ask? – Martin James Jan 31 '16 at 14:50
  • @RichardTingle It's actually an excellent interview question. If the candidate starts to ponder what the result will be, instead of simply dismissing the code as nonsense, they failed. – Lundin Feb 04 '16 at 09:34
  • As for this specific case, it would be interesting to hear what they expected the code to do. `if (a == a)` or `if (a == a+1)` are the two alternatives. I would love to hear how either would make sense. – Lundin Feb 04 '16 at 09:38

2 Answers2

2

The order of evaluation in a==++a is not defined by the standard. Thus the ++ can be performed before the comparison or after comparison. With another compiler you can get different results. This is called 'UB', or 'Undefined Behavior'.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
0

This code will give Undefined Behavior in many ways. But if you initialize a, the difference is that ++a will return the incremented value, while a++ will return the new value.

Also, in for loops, you should use ++a and you will not go wrong.

Let's evaluate your problem.

In the first case, when you compare a with the incremented value of a (as a++ return the incremented value), so it is false. Example: a has 5, and the incremented value is 6. So, as they do not match, it will be a false.

In the second case, when you compare a with the old value of a (as ++a returns the original value), you get true. Example: a has 5 and when you increment it using ++a, you get the old/original value, which is also 5. Thus, you get a true.

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67