f



about "++" and "--"

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--);
}

0
fxc123 (2)
12/1/2005 4:15:57 PM
comp.lang.c++ 49423 articles. 5 followers. Post Follow

8 Replies
2448 Views

Similar Articles

[PageSpeed] 19

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

Ask your compiler-vendor because this result is IMHO implementation-defined.
0
Follow.Me (52)
12/1/2005 4:23:28 PM
Check this out:

http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.15
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.16

Regards,
Irina Marudina

0
izida9 (5)
12/1/2005 4:34:23 PM
fxc123@gmail.com wrote:
> 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.

--
Mike Smith
0
12/1/2005 4:49:08 PM
On 2005-12-01 11:15:57 -0500, "fxc123@gmail.com" <fxc123@gmail.com> said:

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

Read the FAQ:
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.15

>     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--);
> }

(Also, you forgot to give main a return type, or to include <stdio.h> 
or <cstdio>)


-- 
Clark S. Cox, III
clarkcox3@gmail.com

0
clarkcox3 (505)
12/1/2005 7:42:05 PM
On Thu, 01 Dec 2005 17:23:28 +0100, "Oliver S." <Follow.Me@gmx.net>
wrote in comp.lang.c++:

> > why this program snippet display "8,7,7,8,-7,-8"
> 
> 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.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
0
jackklein (3931)
12/2/2005 3:49:47 AM
but I think this problem also relates the sequence of argument into the
"printf" function's stack, is it?

0
fxc123 (2)
12/2/2005 5:57:37 AM
fxc123@gmail.com wrote:
> 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

0
deane_gavin (897)
12/2/2005 11:29:19 AM
fxc123@gmail.com wrote:
> 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] = i++ or a[i++] = i
f(i++,i++)

0
john_bode (637)
12/2/2005 4:45:05 PM
Reply: