[LIP] Undefined behaviour ?

Mithun Bhattacharya <inzoik-/[email protected]>
Newsgroups gmane.user-groups.linux.india.programmers
Message-ID <[email protected]>
-----------------
#include <stdio.h>
main() {
int i = 4;
/* int j = i+++++i+i+++++i; */
/* int j = i++ + ++i + i++ + ++i ; */
int j = (i++)+(++i)+(i++)+(++i);
printf("%d\n", j);
}
-----------------

The above peice of code has three initialization of j out of which only
the last two works with gcc 3.2.2

I am not sure why gcc fails on the first but the error message "invalid
lvalue in increment" doesnt somehow seem right. I dont see the problem
with j and if i++ is being considered a lvalue then why so ?

After compillation I get a value of 21 - how that value is coming up I
have no idea. VC compiles all three syntaxes I believe but returns 21
in debug mode and 24 in release mode.

From my limited knowledge I can think of two ways to process the
expression.

(i++) + (++i) + (i++) + (++i) = (4++) + (++5) + (6++) + (++7) = 4 + 6 +
6 + 8 = 24

(i++) + (++i) + (i++) + (++i) = (4++) + (++4) + (5++) + (++5) = 4 + 5 +
5 + 6 = 20

Obviously none of them match what gcc is coming up with. Could someone
please explain what is happening here ?



Mithun

__________________________________
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/britneyspears/


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?   SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.