473,396 Members | 1,743 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,396 software developers and data experts.

postfix increment acts different then in C# and C/C++

BlankThis maybe a known issue, but I stepped on it just now.
Basically it boils down to: in C#

int x = 0;
Console.Write("{0}", x++ + x++);

gives 1, when in C/C++

int x = 0;
printf("%d\n", x++ + x++);

gives 0, which I think is correct.
What goes the group think about it?

Alex Kosterin
Nov 16 '05 #1
7 2541

"alex" <ak*************@rogers.com> wrote in message
news:Uv********************@rogers.com...
BlankThis maybe a known issue, but I stepped on it just now.
Basically it boils down to: in C#

int x = 0;
Console.Write("{0}", x++ + x++);

gives 1, when in C/C++

int x = 0;
printf("%d\n", x++ + x++);

gives 0, which I think is correct.
What goes the group think about it?

Alex Kosterin


I think C# is correct. When you do the second x++, x should have the value
1.

/ Fredrik
Nov 16 '05 #2
1. I'm surprised at the C/C++ output. How can the compiler do anything
but evaluate each element of the expression in turn, where "evalaute"
includes evaluating the postfix operator, which includes changing the
value. If the expression elements are evaluated left to right, this
gives the following result:

evaluate x++: result is 0, x is changed to 1
evaluate x++: result is 1, x is changed to 2
sum left and right side values: 0 + 1 = 1

I have no idea how the language comes up with anything else. That would
be an interesting question for the comp.lang.c group.

2. For me, this is yet another reason to avoid preincrement,
postincrement, predecrement, and postdecrement. You don't _need_ to use
them: you can just as easily do the same thing with simple mathematics
and assignment statements, and they confuse most programmers. Anyway,
that's just my take on things, which I settled on after scratching my
head over what *p++ does in C. (Does it increment the pointer, or
increment the target of the pointer?)

Nov 16 '05 #3
> BlankThis maybe a known issue, but I stepped on it just now.
Basically it boils down to: in C#

int x = 0;
Console.Write("{0}", x++ + x++);

gives 1, when in C/C++

int x = 0;
printf("%d\n", x++ + x++);

gives 0, which I think is correct.
What goes the group think about it?

There is no correct result for that C++ code.
Its behavior is undefined because x is modified
more than once between sequence points.

As for the C# code, I don't know whether its
behavior is defined or not. I do know that one
has to be deranged to write code like that.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
"alex" <ak*************@rogers.com> wrote in message news:Uv********************@rogers.com...
Nov 16 '05 #4
lex wrote:
BlankThis maybe a known issue, but I stepped on it just now.
Basically it boils down to: in C#

int x = 0;
Console.Write("{0}", x++ + x++);

gives 1, when in C/C++

int x = 0;
printf("%d\n", x++ + x++);

gives 0, which I think is correct.
What goes the group think about it?

Alex Kosterin

x++ + x++ is not valid code, and produces undefined results. So, what you see
is what you get, and may change from instant to instant, version to version, etc.

Review the language on pre/post increment and sequence points.
Nov 16 '05 #5
alex wrote:
BlankThis maybe a known issue, but I stepped on it just now.
Basically it boils down to: in C#

int x = 0;
Console.Write("{0}", x++ + x++);

gives 1, when in C/C++

int x = 0;
printf("%d\n", x++ + x++);

gives 0, which I think is correct.
What goes the group think about it?


It's only "correct" if you consider your C++ implementation to be
authorative. In the standards, that particular expression is undefined
because the variables have multiple side-effects and it's up to the
compiler to arrange the evaluation for optimum performance. This means
that a different C++ compiler, or even the same brand/version of the C++
compiler on a different platform, might produce a different result.

The C# standards only say that the actual operation happens after the
value has been taken, as outlined in topic 7.5.9 of the C# language
specs. It appears here that the entire expression is evaluated before
the variable is incremented. I wouldn't be surprised if somewhere it
said that double side-effects produce undefined results.

What I think of it ? I think that it's a contrived example and any
issues you have with it should be left alone.

In any case, it isn't going to change so the best idea is just to
remember that this is not something that you do and then it's not an
issue any more.

In C++, with macros, this is a more common issue than in C# where macros
are not available.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x0270466B
Nov 16 '05 #6

"alex" <ak*************@rogers.com> wrote in message
news:Uv********************@rogers.com...
BlankThis maybe a known issue, but I stepped on it just now.
Basically it boils down to: in C#

int x = 0;
Console.Write("{0}", x++ + x++);

gives 1, when in C/C++

int x = 0;
printf("%d\n", x++ + x++);

gives 0, which I think is correct.
What goes the group think about it?

Alex Kosterin


1. This is awful code in any language. Avoid it.

2. Awful nowithstanding, it does have a preicsely defined result in C#, and
it is 1. Likewise, x has a defined value as the end of the Write
in C#, and it's 2.

3. This code is undefined in both C and C++. You have no reason to expect
any particular answer in any implementation of wither language.
Nov 16 '05 #7
J. Jones <jj@networld.com> wrote:
x++ + x++ is not valid code, and produces undefined results. So, what you see
is what you get, and may change from instant to instant, version to version, etc.


Not in C# it's not. It's perfectly valid code, and the specification
defines what it should do. In C/C++, it's valid but the result is
undefined.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

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

Similar topics

6
by: Sergey | last post by:
Hello! Could anybody be kind enough to explain this concept? Why C++ make two ops for prefix and postfix ++ operator? As I guess, it is possible to implement both cases via sole prefix...
2
by: Christian Christmann | last post by:
Hi, is there a difference in terms of efficiency when using a prefix "++x;" instead of a postfix "x++;"? Thanks Chris
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 // //...
98
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
2
by: shan | last post by:
Hi to everybody, I am begginer in C programming language. My simple doubt is the difference between postfix & prefix unary operator plus. (i.e) i++ and ++i . plz give me an example program...
13
by: Oleg Kharitonov | last post by:
Hello! I have found an interesting thing with postfix operator ++. What should contain the variable i after exceution the following code: int i = 5; i = i++; In VC++ 7.1 and VC++ 2005...
5
by: mrpermuted | last post by:
Hello, Could someone explain to me why the postfix ++ operator is listed near the top of the operator precedence (above even prefix ++ on my book's chart) but is not evaluated before operators...
8
by: subramanian100in | last post by:
Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference
1
by: Scott Gifford | last post by:
Hello, I'm working on an providing an iterator interface to a database. The basic thing I'm trying to accomplish is to have my iterator read rows from the database and return constructed...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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,...

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.