0

Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)

int main()
{
int a=5,s;
s=++a + ++a;
printf("%d",a);
printf("%d",s);
}

output is 7 and 14

BUT

int main()
{
int a, s;
printf("Enter value of a");
scanf ("%d",&a);
s=++a + ++a;
printf("%d",a);
printf("%d",s);
}

input user gives is 5 output is 7 and 13

WHY?

Community
  • 1
  • 1
codemaniac
  • 879
  • 1
  • 11
  • 31
  • I see `714` for the 2nd example too: http://ideone.com/vJbaH – Blindy Aug 17 '10 at 09:04
  • There are so many duplicates of this on SO. For example: http://stackoverflow.com/questions/2902638/post-and-pre-increment-in-c or http://stackoverflow.com/questions/949433/could-anyone-explain-these-undefined-behaviors-i-i-i-i-i-etc – Naveen Aug 17 '10 at 09:05
  • what C compiler are you using? – Rouan van Dalen Aug 17 '10 at 09:07
  • I really fail to see where the undefined behavior is. You guys need to stop looking at 3 random characters and decide "oh hey i see a ++ twice, it has to be undefined behavior, here I'll link duplicates and call it a day". **It's not** `a=a++ + ++a` – Blindy Aug 17 '10 at 09:08
  • Got the correct results using VC10. – Hamid Nazari Aug 17 '10 at 09:12
  • Yea this code works fine, people in this thread haven't even tested it.. I'm a bit puzzled over why its 14, but both versions return the same result. – Blindy Aug 17 '10 at 09:14
  • @Blindy: It doesn't matter whether you are assigning the result back to `a`. With `a++ + ++a` statement you are modifying the variable `a` twice within a sequence point. Hence it is UB. – Naveen Aug 17 '10 at 09:15
  • Dude read the code he posted. Wish I could give comments a -1 too. – Blindy Aug 17 '10 at 09:15
  • 1
    @Blindy: The `++a + ++a` part alone is sufficient. `a` is modified twice without any intervening sequence point. That's it. See http://c-faq.com/expr/evalorder2.html – jamesdlin Aug 17 '10 at 09:17
  • 1
    It's still undefined behaviour, even with two pre-increments AFAIK - the second pre-increment can happen after the first read or before. Or indeed anything else - as it's undefined. – Douglas Leeder Aug 17 '10 at 09:18
  • @Blindy: I don't know what you see, but I see **two** ++a within the same expression in his code, which means **two** write accesses to a. – Secure Aug 17 '10 at 09:18
  • 4
    It _is_ undefined, Blindy. Appendix C of the standard does _not_ list `+` as a sequence point. It makes no difference whether it's a pre or post increment, it's the dual modification without intervening sequence point that makes it undefined. The correct output is a set consisting of any damn thing the compiler wants to do :-) – paxdiablo Aug 17 '10 at 09:18
  • Why would you ever want to do something like this? – houbysoft Aug 17 '10 at 09:19
  • @houbysoft, you wouldn't, not if you're smart :-) – paxdiablo Aug 17 '10 at 09:20
  • @Blindy: If you want a concrete example why this isn't well-defined, imagine the following sequence: evaluate left `a`, compute `a + 1`, evaluate right `a`, compute `a + 1`, store value for left `a`, store value for right `a`. In that case, the new value of `a` would be only 1 more than it was originally, whereas a strictly left-to-right evaluation would increment `a` twice. – jamesdlin Aug 17 '10 at 09:24

4 Answers4

3

Undefined behaviour:

s=++a + ++a;

Anything can happen when undefined, so your behaviour is perfectly valid.

Secure
  • 4,268
  • 1
  • 18
  • 16
  • 1
    @Blindy: Perhaps you could explain, exactly, what point is missing. – Greg Hewgill Aug 17 '10 at 09:22
  • He's missing the point that regardless of this line, both the OP's versions of the code should (and do) return the same thing on the same compiler. That's what the question is actually about. – Blindy Aug 17 '10 at 09:24
  • @Blindy: That's not what the question says. 14 != 13. – Greg Hewgill Aug 17 '10 at 09:28
  • 4
    No, Blindy, I think you're missing the point: undefined means exactly that, undefined. It's perfectly acceptable for even the first program on its own to give a totally different, random, answer every time you run it. It's also perfectly acceptable for the compiler to format your hard disk, or for space to fold in on itself and form a singularity. The fact that you have the same int going in and the same statement is irrelevant. The compiler is free to do whatever it wants. That's why you _don't use undefined behaviour!_ – paxdiablo Aug 17 '10 at 09:32
  • @Blindy 2 lines with `s = ++a + ++a` don't actually have to behave the same, because the behaviour is undefined by the standard. – Scott Wales Aug 17 '10 at 09:32
1

I'd suspect this is an artifact of compiler optimisation, in the first example a is known so the compiler optimises the preincrements to occur before the addition. In the second example the value is unknown and the compiler does not optimise the sequence causing it to complete left to right. This may be a function of your specific compiler and it would need to be looked at specifically.

Lazarus
  • 41,906
  • 4
  • 43
  • 54
1

Undefined behaviour. Change it, or you risk being attacked by raptors.

pauljwilliams
  • 19,079
  • 3
  • 51
  • 79
0

hi budy this coding working correctly in VI compiler ..

user422650
  • 11
  • 1