Connecting Tech Pros Worldwide Help | Site Map

about "++" and "--"

 
LinkBack Thread Tools Search this Thread
  #1  
Old December 1st, 2005, 03:25 PM
fxc123@gmail.com
Guest
 
Posts: n/a
Default 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--);
}


  #2  
Old December 1st, 2005, 03:35 PM
Oliver S.
Guest
 
Posts: n/a
Default Re: about "++" and "--"

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

Ask your compiler-vendor because this result is IMHO implementation-defined.
  #3  
Old December 1st, 2005, 03:45 PM
Irina Marudina
Guest
 
Posts: n/a
Default Re: about "++" and "--"

Check this out:

http://www.parashift.com/c++-faq-lit...html#faq-39.15
http://www.parashift.com/c++-faq-lit...html#faq-39.16

Regards,
Irina Marudina

  #4  
Old December 1st, 2005, 03:55 PM
Mike Smith
Guest
 
Posts: n/a
Default Re: about "++" and "--"

fxc123@gmail.com wrote:[color=blue]
> 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--);
> }
>[/color]

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

--
Mike Smith
  #5  
Old December 1st, 2005, 06:55 PM
Clark S. Cox III
Guest
 
Posts: n/a
Default Re: about "++" and "--"

On 2005-12-01 11:15:57 -0500, "fxc123@gmail.com" <fxc123@gmail.com> said:
[color=blue]
> why this program snippet display "8,7,7,8,-7,-8"[/color]

Read the FAQ:
http://www.parashift.com/c++-faq-lit...html#faq-39.15
[color=blue]
> 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--);
> }[/color]

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


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

  #6  
Old December 2nd, 2005, 02:55 AM
Jack Klein
Guest
 
Posts: n/a
Default Re: about "++" and "--"

On Thu, 01 Dec 2005 17:23:28 +0100, "Oliver S." <Follow.Me@gmx.net>
wrote in comp.lang.c++:
[color=blue][color=green]
> > why this program snippet display "8,7,7,8,-7,-8"[/color]
>
> Ask your compiler-vendor because this result is IMHO implementation-defined.[/color]

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/~a...FAQ-acllc.html
  #7  
Old December 2nd, 2005, 05:05 AM
fxc123@gmail.com
Guest
 
Posts: n/a
Default Re: about "++" and "--"

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

  #8  
Old December 2nd, 2005, 10:35 AM
deane_gavin@hotmail.com
Guest
 
Posts: n/a
Default Re: about "++" and "--"


fxc123@gmail.com wrote:[color=blue]
> but I think this problem also relates the sequence of argument into the
> "printf" function's stack, is it?[/color]

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

  #9  
Old December 2nd, 2005, 03:55 PM
john_bode@my-deja.com
Guest
 
Posts: n/a
Default Re: about "++" and "--"


fxc123@gmail.com wrote:[color=blue]
> 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--);
> }[/color]

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++)

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.