473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

Dec 1 '05 #1
8 1373
> why this program snippet display "8,7,7,8,-7,-8"

Ask your compiler-vendor because this result is IMHO implementation-defined.
Dec 1 '05 #2
fx****@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
Dec 1 '05 #4
On 2005-12-01 11:15:57 -0500, "fx****@gmail.com" <fx****@gmail.com> said:
why this program snippet display "8,7,7,8,-7,-8"
Read the FAQ:
http://www.parashift.com/c++-faq-lit...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
cl*******@gmail.com

Dec 1 '05 #5
On Thu, 01 Dec 2005 17:23:28 +0100, "Oliver S." <Fo*******@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/~a...FAQ-acllc.html
Dec 2 '05 #6
but I think this problem also relates the sequence of argument into the
"printf" function's stack, is it?

Dec 2 '05 #7

fx****@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

Dec 2 '05 #8

fx****@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++)

Dec 2 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
8
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
77
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is...
0
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
7
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.