473,216 Members | 2,240 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,216 software developers and data experts.

Funny results with increment decrement operators

Could some C guru please help ? I have:

int x = 20, y = 35;
x = x++ + y++;
y = ++x + ++y;
printf("%d %d\n", x, y);

The answers are 57, 94

I tried reasoning using the standard C semantics that ++x means 'first
increment and then do something else', as copared to x++ which is
'first do something else and then increment'.
So, x = x++ + y++; results in x = (20 + 35) + 1 = 56 and y = 36
and then y = ++x + ++y; results in (56+1) + (36+1) = 57 + 37 = 94
Where is the 57 coming from, for x = x++ + y++;

I am using gcc. Thanks in advance for your helpful hints.
Feb 16 '08 #1
8 3679
On Feb 16, 12:00 am, "cpptutor2...@yahoo.com" <cpptutor2...@yahoo.com>
wrote:
Where is the 57 coming from, for x = x++ + y++;
Ah, y'see. What you have going on here is "undefined behaviour".
Basically, the compiler can make up any value it likes, and you can't
complain. See
http://en.wikipedia.org/wiki/Undefined_behavior for some more info.
This instance of it involves a no-no with sequence points.
Feb 16 '08 #2
cp**********@yahoo.com wrote:
Could some C guru please help ? I have:
You could have helped your self by looking back less than a day in this
group.

--
Ian Collins.
Feb 16 '08 #3
I tried reasoning using the standard C semantics that ++x means 'first
increment and then do something else', as copared to x++ which is
'first do something else and then increment'.
So, x = x++ + y++; results in x = (20 + 35) + 1 = 56 and y = 36
and then y = ++x + ++y; results in (56+1) + (36+1) = 57 + 37 = 94
Where is the 57 coming from, for x = x++ + y++;

I am using gcc. Thanks in advance for your helpful hints.
Off-topic slightly but why do this constantly come up? Do people actually
try coding like this? and get stuck wondering why it doesn't work. Why don't
people just save them and everyone and just put i++; on the next statement.
Feb 16 '08 #4
In article <47***********************@news.optusnet.com.au> ,
MisterE <vo***@sometwher.worldwrote:
>So, x = x++ + y++; results in x = (20 + 35) + 1 = 56 and y = 36
and then y = ++x + ++y; results in (56+1) + (36+1) = 57 + 37 = 94
>Off-topic slightly but why do this constantly come up? Do people actually
try coding like this?
Many of them come from stupid puzzles set by uninspired teachers and
interviewers.

In real life, people rarely write examples as obvious as the ones
above. But more complicated examples where the side effect is buried
in array subscripts and the like are probably more common, and
invisible examples in macros even more so.

-- Richard
--
:wq
Feb 16 '08 #5
"cp**********@yahoo.com" wrote:
>
Could some C guru please help ? I have:

int x = 20, y = 35;
x = x++ + y++;
y = ++x + ++y;
printf("%d %d\n", x, y);

The answers are 57, 94
The answers are correct, as are any other answers. The program
invokes undefined behaviour.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Feb 18 '08 #6
Thad Smith wrote:
>
.... snip ...
>
In the case mentioned, there is a readily available standard for
C, but many people don't know it exists, don't know how get it,
or don't know how to interpret it. There is the additional
important question of whether the compiler you are using
supports a particular standard feature.
If the compiler doesn't include any standard C features, it is
deficient, and you are fully justified in demanding your money
back. Exception - when it publishes its deficencies, as does gcc.

Some useful references about C (C99 are standards):
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://c-faq.com/ (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf(C99)
<http://cbfalconer.home.att.net/download/n869_txt.bz2(C99, txt)
<http://www.dinkumware.com/refxc.html (C-library}
<http://gcc.gnu.org/onlinedocs/ (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Feb 18 '08 #7
CBFalconer <cb********@yahoo.comwrites:
[...]
Some useful references about C (C99 are standards):
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://c-faq.com/ (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf(C99)
<http://cbfalconer.home.att.net/download/n869_txt.bz2(C99, txt)
<http://www.dinkumware.com/refxc.html (C-library}
<http://gcc.gnu.org/onlinedocs/ (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>
Let me point out one more time that n869_txt.bz2 is not a standard;
it's a draft of the C99 standard. Its only advantage is that (once
you decompress it with bunzip2) it's plain text; that can also be a
disadvantage, since some important formatting information is lost (for
example, definitions of terms are indicated with italics). Some
changes were made between n869 and the official release of the C99
standard. Still more post-C99 changes were made in three Technical
Corrigenda, which are incorporated into n1256.pdf.

My advice: consider using n869 *only* if the ability to use plain text
rather than PDF is very important to you. If you have a decent PDF
reader and don't mind using it, use n1256.pdf.

Yes, n1256 is also a draft, but it incorporates the official C99
standard and all three official technical corrigenda. If you're even
more of a sticker for accuracy than I am (wow!), then you can pay for
a copy of the actual C99 standard ($18 when I bought it, probably a
little more now) and obtain all three TCs at no charge.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 18 '08 #8
On Feb 15, 7:00 pm, "cpptutor2...@yahoo.com" <cpptutor2...@yahoo.com>
wrote:
Could some C guru please help ? I have:

int x = 20, y = 35;
x = x++ + y++;
y = ++x + ++y;
printf("%d %d\n", x, y);

The answers are 57, 94

I tried reasoning using the standard C semantics that ++x means 'first
increment and then do something else', as copared to x++ which is
'first do something else and then increment'.
You have the semantics slightly wrong. The expression "++x" evaluates
to the current value of x + 1, and as a *side effect*, x will be
incremented. Exactly *when* x gets incremented is somewhat variable,
as long as it occurs before the next sequence point. So x may be
incremented immediately after the expression is evaluated, or it may
not be incremented until after all expressions have been evaluated.

For example, given the statement

x = y++ * --z;

the compiler is free to evaluate it as follows:

t1 <- z - 1
t2 <- y
x <- t2 * t1
y <- y + 1
z <- z - 1

Attempting to modify an object more than once between sequence points
invokes undefined behavior; *any* result is correct.

Go to http://www.c-faq.com and read Section 3, and pay particular
attention to question 3.8.

Any expression of the forms

x = x++
x++ + x++
a[i] = i++

will invoke undefined behavior.

Feb 18 '08 #9

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

Similar topics

9
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a...
8
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
11
by: John | last post by:
Hi, I encountered a strange problem while debugging C code for a Windows-based application in LabWindows CVI V5.5, which led me to write the test code below. I tried this code with a different...
5
by: Ian Pilcher | last post by:
I'm trying to figure out if an increment to a variable of an integer type, followed by a decrement, (or vice versa) is guaranteed to restore the variable to its initial value, even if the first...
8
by: Angel Tsankov | last post by:
Should pre/post increment/decrement return const or non-const? What about other functions?
5
by: Stuart | last post by:
Hi all, Iv'e got a page that has a mass amount of input fields, all of which require a decimal figure. To make it easier when it comes to inputting data, I'm trying to setup + and - links that...
3
by: shivapadma | last post by:
i want clear explanation of increment and decrement operators in c. can any body help in solving the following expression a=10 c=++a + ++a when i am executing this code ,the answer is c=24...
6
by: Kevin Walzer | last post by:
This code: #include <stdio.h> int main(void) { int n1, n2; //two integers n1 = 1; n2 = 1;
3
by: Stang1 | last post by:
The following statement: line_buf = ' '; is equivalent to: line_buf = ' '; line_len++;
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.