Output is 19..Can anyone explain how?Code:#include<stdio.h> int main() { int x=8; x = --x + x-- + x--; printf("%d\n",x); return 0; }
Output is 19..Can anyone explain how?Code:#include<stdio.h> int main() { int x=8; x = --x + x-- + x--; printf("%d\n",x); return 0; }
Thanks but why undefined??? Can you explain please?
Because the C standard says that a variable should only be changed once within a sequence point (basically, within one statement, although that's a simplification). This is so that the compiler doesn't HAVE to do these type of things in a particular order, which in turn allows the compiler to use more efficient instructions in some special cases [1]. And this is why the value from such an expression will vary from one compiler to another (or between different versions of compilers, different compiler settings, etc).
[1] For example, 68K and PDP-11/VAX-11 have "autoincrement" and "autodecrement" instruction modes that automatically update the pointer of a memory location, as well as store or retrieve the data in memory. Using *ptr++ can then be implemented as a autoincrement type operation. But if the compiler had to take into account the entire line of execution before using such operations, it may not be able to use them very often.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Thanks alot