Calculation of increment operator

L

laikon

Hello, everyone, below is the program to calculate an expression of
increment operator, the result j is 4 and i is 8 seems unbelieveable,
so what is the rule of such expression?

int i , j = 2;
i = ++j+(++j);

and what if
i = (j++)+(j++);
i = (j++)+(++j);

thanks for explanation!
 
M

Mike Wahler

laikon said:
Hello, everyone, below is the program to calculate an expression of
increment operator, the result j is 4 and i is 8 seems unbelieveable,
so what is the rule of such expression?

int i , j = 2;
i = ++j+(++j);

and what if
i = (j++)+(j++);
i = (j++)+(++j);

There ain't no rules. The behavior is undefined.

-Mike
 
X

Xeranic

laikon said:
Hello, everyone, below is the program to calculate an expression of
increment operator, the result j is 4 and i is 8 seems unbelieveable,
so what is the rule of such expression?

int i , j = 2;
i = ++j+(++j);

and what if
i = (j++)+(j++);
i = (j++)+(++j);

thanks for explanation!

Never do things like this. I mean don't increase/decrease a var more
than one times in single expression. The beheave is different in
different compiler.
 
I

Ivan Novick

int i , j = 2;
i = ++j+(++j);

and what if
i = (j++)+(j++);
i = (j++)+(++j);

If you have access to gcc, try compiling these with -Wall compiler
option and you should get a warning saying "operation on 'j' may be
undefined".

The reason is the C++ standard states in section 5.4 : "Between the
previous and next sequence point a scalar object shall have its stored
vale modified at most once by the evaluation of an expression".

-
Ivan
http://www.0x4849.net
 
S

Salt_Peter

laikon said:
Hello, everyone, below is the program to calculate an expression of
increment operator, the result j is 4 and i is 8 seems unbelieveable,
so what is the rule of such expression?

int i , j = 2;
i = ++j+(++j);

and what if
i = (j++)+(j++);
i = (j++)+(++j);

thanks for explanation!

The result is undefined. Even if it did what you expected, its still
undefined.
 
J

Jim Langston

laikon said:
Hello, everyone, below is the program to calculate an expression of
increment operator, the result j is 4 and i is 8 seems unbelieveable,
so what is the rule of such expression?

There is no rule, it is undefined, but I can explain how you got j as 4 and
i as 8.
int i , j = 2;
i = ++j+(++j);

Okay, say the compiler first looks at the ++j's and does them. 2+1 = 3 3+1
= 4. Now it uses the values, so it becomes
i = 4 + 4

now i is 8, j is 4.

Although with a different compiler you could get different results. (Maybe
i = 6, j = 3 or i = 7 j = 4 or anything else, it's undefined).
 
F

Frederick Gotham

laikon:
Hello, everyone, below is the program to calculate an expression of
increment operator,


That's a lie and we both know it.

the result j is 4 and i is 8 seems unbelieveable,


To a novice programmer, perhaps.

so what is the rule of such expression?

int i , j = 2;
i = ++j+(++j);

and what if
i = (j++)+(j++);
i = (j++)+(++j);


This is in the FAQ somewhere. Maybe try Google for "sequence point".
 
H

Howard

Frederick Gotham said:
laikon:



That's a lie and we both know it.

Don't be a jerk. His English doesn't appear to be too good, but do you
seriously think it was his intention to "lie"? (If you're just making a
joke, how about adding a smiley or something?)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top