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

A

alex

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
 
F

Fredrik Wahlgren

alex said:
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
 
B

Bruce Wood

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?)
 
L

Larry Brasfield

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.
 
J

J. Jones

lex said:
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.
 
?

=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

alex said:
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.
 
M

Mike Schilling

alex said:
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.
 
J

Jon Skeet [C# MVP]

J. Jones said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top