++i operator difference in C# and C++
Asked Answered
A

4

14

I have the following code written in both C++ and C#

 int i=0;
 ++i = 11;

After this C# compiler brings an error

The left-hand side of an assignment must be a variable, property or indexer

But C++ compiler generate this code with no error and I got a result 11 for value of i. What's the reason of this difference?

Adal answered 1/3, 2012 at 13:18 Comment(4)
offtopic: why do you want to increment it and then directly set it to 11? The increment makes no sense.Rockbound
What is the use for this code?Parra
someArray[++i] = 11 would make more sense.Freud
It's just interesting to me. @mesiesta explained correctly why is it so.Adal
V
42

The difference is that pre-increment operator is lvalue in C++, and isn't in C#.
In C++ ++i returns a reference to the incremented variable. In C# ++i returns the incremented value of variable i.
So in this case ++i is lvalue in C++ and rvalue in C#.

From C++ specification about prefix increment operator

The type of the operand shall be an arithmetic type or a pointer to a completely-defined object type. The value is the new value of the operand; it is an lvalue.

P.S. postfix increment operator i++ isn't lvalue in both C# and C++, so this lines of code will bring error in both languages.

 int i=0;
 i++ = 11;
Vaduz answered 1/3, 2012 at 13:19 Comment(0)
R
6

Note that ++i = 11 invokes undefined in C++03 because you are modifying i twice without an intervening sequence point. It is well defined in C++11, however: first the increment, then the assignment.

Robomb answered 1/3, 2012 at 15:45 Comment(0)
L
5

C# and C++ are 2 totally different languages and concepts. They only share the name because their syntax is based on C. So actually "why this works on C# but not on C++" makes no sense as a question. It's the same as saying why a table is called "table" in English, but "mesa" in Spanish. Because it was decided to be that way.

C# simply does not allow such syntax.

In C++ you're allowed to: first ++i is computed, which makes i = 1, and then 11 is assigned to i, which makes i = 11.

Lithography answered 1/3, 2012 at 13:20 Comment(4)
That's a bit harsh. The question is common to the C language and many things that work in C++ also work in C#! It's a perfectly valid question IMO, particularly since in C++ the code in question represents a statement that makes no logical sense.Canton
Harsh? Why harsh? I'm just stating that C# and C++ have nothing in common except being based on C. The question is not common to C, because he's not asking about C, and in C that sentence works perfectly, and it does not in C#, which just demonstrates C# has nothing to do with C except the name and the syntax (which btw is copied by a lot of language like Perl, Java and such).Lithography
C# and C++ have similarities... There aren't different like for example Java and Haskell. So for me it's a valid question and it's interesting for me to know.Vaduz
They're different like Java and Haskell, but I'm not going to state all the differences and the very few similarities. It's indeed interesting to know, but I personally wouldn't have formulated the question that way.Lithography
I
2

the semantics are very different. In c++ the semantics are that you assign the value 11 to the storage location identified by i. In C# the semantics are equal to the semantics of the below statement

1=11

That is it's equal to trying to assign the value 11 to the value 1 which the C# compiler does not allow. (Fortran compilers actually do allow this and that can create hellish debugging scenarios)

Important answered 1/3, 2012 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.