about "++" and "--"

F

fxc123

why this program snippet display "8,7,7,8,-7,-8"
the program is:

main()
{
int i=8;
printf("%d\n%d\n%d\n%d\n%d\n%d\n",++i,--i,i++,i--,-i++,-i--);
}
 
O

Oliver S.

why this program snippet display "8,7,7,8,-7,-8"

Ask your compiler-vendor because this result is IMHO implementation-defined.
 
M

Mike Smith

why this program snippet display "8,7,7,8,-7,-8"
the program is:

main()
{
int i=8;
printf("%d\n%d\n%d\n%d\n%d\n%d\n",++i,--i,i++,i--,-i++,-i--);
}

There is no guarantee that that program will produce that output,
because it relies on undefined behavior.
 
J

Jack Klein

Ask your compiler-vendor because this result is IMHO implementation-defined.

No, it is most specifically undefined behavior. Which means that the
C++ language, and we, don't know or care what happens.
 
F

fxc123

but I think this problem also relates the sequence of argument into the
"printf" function's stack, is it?
 
D

deane_gavin

but I think this problem also relates the sequence of argument into the
"printf" function's stack, is it?

That might explain what your particular implementation was doing. The
compiler is allowed to evaluate the arguments to a function in any
order it likes.

But that's not the point. There is no sequece point between evaluating
each argument. Your code

int i=8;
printf("%d\n%d\n%d\n%d\n%d\n%d\n",++i,--i,i++,i--,-i++,-i--);

modifies i more than once without an intervening sequence point. That
is undefined behaviour - which means that anything can happen. If you
are lucky when you invoke undefined behavior you get a crash or some
other obvious signal that there's something wrong with your program. If
you are unlucky, as you are in this case, you get some sort of
seemingly reasonable behaviour you think you can explain.

Gavin Deane
 
J

john_bode

why this program snippet display "8,7,7,8,-7,-8"
the program is:

main()
{
int i=8;
printf("%d\n%d\n%d\n%d\n%d\n%d\n",++i,--i,i++,i--,-i++,-i--);
}

Attempting to update an object more than once between sequence points
leads to undefined behavior, so *any* output is allowed.

Remember that i++ reads "evaluate to the current value of i, and
sometime before the next sequence point increment i by one."
Similarly, --i reads "evaluate to the current value of i - 1, and
sometime before the next sequence point decrement i by 1."

The "sometime before the next sequence point" part is the problem;
there is no explicit requirement on exactly *when* to apply the side
affect, as long as it occurs before the next sequence point. The
compiler implementor may choose to apply all side affects immediately
after each expression is evaluated, or defer them until just before the
sequence point, or some combination of the two depending on
circumstances.

So, in short, remember that any expressions of the following forms
invoke undefined behavior:

i = i++
a = i++ or a[i++] = i
f(i++,i++)
 

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,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top