It was played with this code on VS:

int a; int b = !!!!!!!!!!!!!!!!!!!!!!!!!!a; int c = ++++++++++++++++++++++++++a; int d = --------------------------a; 

And that's what happened - if you compile as C, it says that for ++ and -- left-hand value -- required. But if you compile as C ++, it compiles without errors.

I do not understand what the difference is, help me figure it out.

  • one
    Why so much extra code? To demonstrate the question, int a; int c = ++++a; int a; int c = ++++a; . See How to create a minimal, self-contained and reproducible example . - PinkTux
  • @PinkTux: On SO, only specifically asked questions are discussed, and for everything else, there are buttons and one of them is a “edit” that is moderated by the community. - sys_dev

1 answer 1

In C ++, the value of the prefix (unary) operator ++ and - is an lvalue , while in C it is an rvalue , that is, a temporary object that cannot be changed.

Also compare for example the assignment operator. In C ++ you can write like this

 int i; int j = 10; ( i = 5 ) += j; 

In C, however, such code will not be executed as the assignment operator returns an rvalue .

From standard C (6.5.3.1 Prefix increment and decrement operators)

2 The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation. The expression ++ E is equivalent to (E + = 1). And the operators of the association.

V (6.5.16 Assignment operators)

3 An assignment operator value of the left operand. An assignment of the operand after the assignment, 111) but is not an lvalue .

From standard C ++ (5.3.2 Increment and decrement)

1 it is a bool (this use is deprecated). The operand shall be a modifiable lvalue. Completely completely completely completely completely completely The result is the updated operand; it is a bit field if it is a bit field. If it is not the type of bool, it is equivalent to the x + = 1. —End note]

As for the logical negation operator, it applies to expressions and is equivalent to the expression e == 0

From standard C (6.5.3.3 Unary arithmetic operators)

9 The operand of the logical negation operator! is contextually converted to bool (Clause 4); its value is true if it is converted. The type of the result is bool.

And from the standard C ++ (5.3.1 Unary operators)

9 The operand of the logical negation operator! is contextually converted to bool (Clause 4); its value is true if it is converted. The type of the result is bool.

In C programs, you can often find double negation applied to the expression

 !!expression 

This is done so that the result of the expression is exactly 0 or 1 .

  • It's difficult for me to wade through these quotes in English, but I understood the main thing .. Only if it is not a completely stupid question - why? I demal that C ++ to the maximum was made similar to C, at least in basic things - is there such a difference? Why did C ++ go differently than in C? - Mikhailo
  • @Mikhailo I can't answer this question right now. But, I think, in the book of Straustrup, where he writes about the evolution of C ++, which I do not have at hand now, probably the reason is indicated. It is possible that this is somehow connected with the possibility of operator overloading in C ++, in order, for example, to avoid unnecessary copying of the temporary object being created. - Vlad from Moscow
  • four
    @Mikhailo, the difference is simple: links. In C ++, they are, but in C they are not. Without references, you cannot implement ++ as lvalue. Of course, the standard could require such behavior and the compilers would do it, but I suspect that ++i did return lvalue solely for the sake of overloaded versions of it, and nobody began to do the reverse port in C - there’s no point in breaking, works. Moreover, for C it does not make much sense. - ixSci
  • @ixSci Thank you! - Mikhailo