-2

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

main()
{
   int a=5;
   a= a++ + ++a + ++a + a++ + a++;
   printf("%d",a);
}
Community
  • 1
  • 1
prathamesh
  • 23
  • 1

3 Answers3

4

This is not defined.

You can find the Committee Draft from May 6, 2005 of the C-standard here (pdf)

See section 6.5 Expressions:

2 Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

and the example:

71) This paragraph renders undefined statement expressions such as

i = ++i + 1;

a[i++] = i;

Lucas
  • 13,679
  • 13
  • 62
  • 94
  • Robert, I think it is up to the compiler to decide which operator takes precedence in that case (I think I read somewhere that the spec doesn't say). – Graham Edgecombe May 29 '10 at 12:30
  • Thanks, I deleted my edit because precedence came into my mind... – Robert May 29 '10 at 12:32
  • 2
    @Robert: Because that is how C is defined (by its standard): the outcome of an expression which modifies the same thing multiple times is undefined. In practice anyone implementation tends to be consistent, but strictly speaking you cannot rely on even that. – Richard May 29 '10 at 12:33
  • both Dev-cpp and gcc turn up the same answer. – prathamesh May 29 '10 at 12:33
  • whereas the modified code like: printf("%d",a++ + ++a + ++a + a++ + a++); printf("\n%d",a); prints the answer as 33 and value of a as 10. a=10 is justifiable, what about 33 as answer!! – prathamesh May 29 '10 at 12:44
  • @prathamesh: It doesn't matter. It is not defined in the C-language. gcc allows you to look at the assembler that is produced with the `-S` flag. You can then see how gcc interprets it and why you get the output `36`. But it is still not defined and you still shouldn't use it. – Lucas May 29 '10 at 12:45
  • so how's the operator precedence in C?? – prathamesh May 29 '10 at 12:48
  • @prathamesh 2nd comment: Everything is justifiable. If it is `10`, `33`, `36`, `1000` or `123456` – Lucas May 29 '10 at 12:50
  • @Lucas it means it's irregular in nature. isn't there any way to look at it, so as to come up with some conclusive statements? – prathamesh May 29 '10 at 12:54
  • @prathamesh: I added the relevant part of the standard to my answer. You will also find everything about precedence of operators in that section. As to why, you get the result: You will have to look a the assembler as I stated earlier. Although I think it would be of little value, since you cannot rely on this compiler behavior in any way. – Lucas May 29 '10 at 13:09
  • thank you @Lucas....i got some more insight. – prathamesh May 29 '10 at 13:11
1

The answer is actually undefined.

johannes
  • 15,807
  • 3
  • 44
  • 57
-1

Answer in undefined because you've got some situations in which the parser doesn't know how to parse the code..

is a+++b: a + ++b or a++ + b?

Think the fact that usually white space is just ignored when lexing the source code. It may depends upon implementation of the compiler (and some other languages with same ++ operators may choose to give priority to one instead of another) but in general this is not safe.

For example in Java your code line gives 37 as the answer, because it chooses to bind ++ operators in a specific way according to precedence, but it's just a choice..

Jack
  • 131,802
  • 30
  • 241
  • 343
  • 1
    Not true. It's well defined where these tokens start and where they end. In `a+++b` this is `a++ + b` (maximal munch principle). – Johannes Schaub - litb May 29 '10 at 12:34
  • Oh got it, the undefinition given by the same variable modified multiple times. I thought that my point should be considered too by the way, since it's true :) – Jack May 29 '10 at 12:35
  • Yeah, it's well defined in an specific implementation but it may change, we spoke about it in a different question.. let me look for it – Jack May 29 '10 at 12:36
  • whereas the modified code like: printf("%d",a++ + ++a + ++a + a++ + a++); printf("\n%d",a); prints the answer as 33 and value of a as 10. a=10 is justifiable, what about 33 as answer!! – prathamesh May 29 '10 at 12:40
  • Because you are not assigning to the variable you are incrementing but just prints out the whole sum. – Jack May 29 '10 at 12:56