One code, two different results - C# vs C++
-
As I was practicing my C# skills I came across the code below: ```csharp int myFunc() { int x; x = 42; x++; x += --x; return x; } ``` What is the value returned from `myFunc()`? As a C++ programmer I thought: "`84` of course". But when I compiled with C#, the function returns `85`. Just to confirm, I also compiled the same code with Java and returned `85` too. Well... Can anyone explain this? :P
C. Augusto Proiete https://augustoproiete.net
-
As I was practicing my C# skills I came across the code below: ```csharp int myFunc() { int x; x = 42; x++; x += --x; return x; } ``` What is the value returned from `myFunc()`? As a C++ programmer I thought: "`84` of course". But when I compiled with C#, the function returns `85`. Just to confirm, I also compiled the same code with Java and returned `85` too. Well... Can anyone explain this? :P
C. Augusto Proiete https://augustoproiete.net
:confused: I don't see how C++ would return 84??
int x;
x = 0x = 42;
x = 42x++;
x = 43x += --x;
==x = x + (--x)
==x = 43 + 42
x = 85 Is there something I'm missing?? Admittedly, I haven't done C++ in a while Nick Seng (the programmer formerly known as Notorious SMC)
God, I pity me! - Phoncible P. Bone
-
:confused: I don't see how C++ would return 84??
int x;
x = 0x = 42;
x = 42x++;
x = 43x += --x;
==x = x + (--x)
==x = 43 + 42
x = 85 Is there something I'm missing?? Admittedly, I haven't done C++ in a while Nick Seng (the programmer formerly known as Notorious SMC)
God, I pity me! - Phoncible P. Bone
Nick Seng wrote: x += --x; == x = x + (--x) That should be: x--; (42) x = x + x; (84) leppie::AllocCPArticle(Generic DFA State Machine for .NET);
-
Nick Seng wrote: x += --x; == x = x + (--x) That should be: x--; (42) x = x + x; (84) leppie::AllocCPArticle(Generic DFA State Machine for .NET);
hmmm..... Shouldn't it be left to right(order of operation) ? I mean:
x = x + (--x)
: At the beginning of this line x is still 43 x =x
+ (--x) : which would imply that this x is still 43 thus, you would get 85? [Edit]Something just occured to me, does, --x imply that the incrementer execute before any other operators? [/Edit] Nick Seng (the programmer formerly known as Notorious SMC)
God, I pity me! - Phoncible P. Bone
-
hmmm..... Shouldn't it be left to right(order of operation) ? I mean:
x = x + (--x)
: At the beginning of this line x is still 43 x =x
+ (--x) : which would imply that this x is still 43 thus, you would get 85? [Edit]Something just occured to me, does, --x imply that the incrementer execute before any other operators? [/Edit] Nick Seng (the programmer formerly known as Notorious SMC)
God, I pity me! - Phoncible P. Bone
The -- operator has precedence over the += so it goes first makeing x 42 then the += is 42 + 42. Maybe this is a bug in c#? -- is supposed to go first in c# also. Maybe it doens't update x until the whole statement is done, and only returns 42 to the statement so you get 43 + 42.
-
The -- operator has precedence over the += so it goes first makeing x 42 then the += is 42 + 42. Maybe this is a bug in c#? -- is supposed to go first in c# also. Maybe it doens't update x until the whole statement is done, and only returns 42 to the statement so you get 43 + 42.
Hi! I've posted this same question in the Microsoft C# Newsgroups and they said that the operator `+=` has precedence over the `--` because C# goes from left to right... (instead of C++ that goes from the right to the left) So the expression: ```csharp x += --x ``` Would be: ```csharp x = --(x + x) = --(43 + 43) = 85 ``` Nice... but weird :P
C. Augusto Proiete https://augustoproiete.net
-
Nick Seng wrote: x += --x; == x = x + (--x) That should be: x--; (42) x = x + x; (84) leppie::AllocCPArticle(Generic DFA State Machine for .NET);
The decrement operator ( -- ) decrements its operand by 1. The decrement operator can appear before or after its operand: The first form is a prefix decrement operation. The result of the operation is the value of the operand after it has been decremented. The second form is a postfix decrement operation. The result of the operation is the value of the operand before it has been decremented. Bo Hunter
-
Hi! I've posted this same question in the Microsoft C# Newsgroups and they said that the operator `+=` has precedence over the `--` because C# goes from left to right... (instead of C++ that goes from the right to the left) So the expression: ```csharp x += --x ``` Would be: ```csharp x = --(x + x) = --(43 + 43) = 85 ``` Nice... but weird :P
C. Augusto Proiete https://augustoproiete.net
Thanks for the info. I don't know if I agree with that though. Whether you eval from left to right or right to left, you still should evaluate in the proper precedence. The c# spec says that -- has precedence over +=. If you are interested it is in the spec section 7.2.1