0

Can anyone tell me the correct procedure of evaluation of the answer (with internal working)?

#include<stdio.h>
main()
{
int a=10;
printf("%d %d %d\n",a,a++,++a);
return 0;
}
Rubens
  • 14,478
  • 11
  • 63
  • 92
Aman
  • 25
  • 1
  • 1
  • 6
  • 1
    Welcome to Stack Overflow. Please read the [About] page soon. You've invoked undefined behaviour by modifying `a` twice between sequence points, so anything can happen and it is fine. Minor variants on this question are frequently asked; expect it to be closed as a duplicate shortly. – Jonathan Leffler Aug 12 '13 at 16:57

2 Answers2

0

There is no single, correct behavior for this code.
Technically, ANY output or result from this program is correct, even nasal demons.

It falls into undefined behavior.

While we can guess at likely and possible outcomes, anything is allowed by the C language in this case.

abelenky
  • 63,815
  • 23
  • 109
  • 159
  • This answer does not state why the behavior is undefined. – Eric Postpischil Aug 12 '13 at 17:30
  • Please use gcc (gcc-4.8.1) to tell. Answer is 12 11 12 so please tell me the flow control(using stack or any other logic) – Aman Aug 12 '13 at 17:36
  • 1
    Even if we did, that version of gcc can do whatever it pleases when UB is invoked; it doesn't even have to do the same thing every time. – Dennis Meng Aug 12 '13 at 17:51
  • 1
    @Aman Please read http://en.wikipedia.org/wiki/Undefined_behavior , especially “… unpredictable … the programmer can't predict what will happen”. – Pascal Cuoq Aug 12 '13 at 18:53
0

Within a single portion1 of an expression, you are not permitted (within defined behavior of C) to modify an object twice or to modify an object and separately use its value.

Your expression, printf("%d %d %d\n",a,a++,++a), contains a, a++, and ++a. This is not permitted because both a++ and ++a modify a. It is also not permitted because a uses a and a++ separately modifies a.

Do not write code like this.

When you do this, the behavior is undefined. A compiler might evaluate these three things in any order, or it might “break” and do something completely different. You might have gotten “12 11 12“ because the code generated by the compiler did this:

  • First, record the value that a++ would have (11) and calculate the incremented value (12), but hold that value in a temporary register (do not write it to memory yet).
  • Second, record the value that ++a would have (12, because a is still 11).
  • Third, update the value of a, either from the temporary or from the ++a or both.
  • Fourth, record the value that a has (now 12).
  • Fifth, pass these values, 12, 11, and 12, to printf.

But the code might have done something else. In this case, you could tell only by examining the generated assembly code. You cannot tell from what the C standard says, because the C standard does not say what happens in this case. Examining the assembly code would only tell you what happens in this case; it may be different when the same C source code is compiled for a different processor architecture, when different compilation switches are used, or when the compiler version is changed.


1Expressions may be divided into portions by sequence points. See this answer for more about sequence points.

Community
  • 1
  • 1
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Not true, you are permitted, even if the behavior is undefined. – David Ranieri Aug 13 '13 at 14:10
  • @AlterMann: Sure, and you “are permitted” to write complete gobbledygook and feed it to the compiler. Obviously you “are permitted” to do anything, but the context here is that it is not permitted within the confines of what the C standard defines. This answer explicitly states “When you do this, the behavior is undefined”. It is not helpful to make philosophical or nit-picky claims about language. It is important to emphasize to learners that this is code to be avoided. – Eric Postpischil Aug 13 '13 at 14:15