Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are multiple pre-increments allowed in C++ but not in C? [duplicate]

Why is

int main() {     int i = 0;     ++++i; } 

valid C++ but not valid C?

like image 695
code_dragon Avatar asked Jan 11 '18 09:01

code_dragon


People also ask

How does pre increment work in C?

1) Pre-increment operator: A pre-increment operator is used to increment the value of a variable before using it in an expression. In the Pre-Increment, value is first incremented and then used inside the expression.

How multiple increment and decrement operators work in C?

C has two special unary operators called increment ( ++ ) and decrement ( -- ) operators. These operators increment and decrement value of a variable by 1 . Increment and decrement operators can be used only with variables. They can't be used with constants or expressions.

How pre increment and post increment works in C?

Pre-increment and Post-increment concept in C/C++? Decrement operator decrease the value by one. Pre-increment (++i) − Before assigning the value to the variable, the value is incremented by one. Post-increment (i++) − After assigning the value to the variable, the value is incremented.

Can you increment a number in C?

A program can increment by 1 the value of a variable called c using the increment operator, ++, rather than the expression c=c+1 or c+=1. An increment or decrement operator that is prefixed to (placed before) a variable is referred to as the prefix increment or prefix decrement operator, respectively.


2 Answers

C and C++ say different things about the result of prefix ++. In C++:

[expr.pre.incr]

The operand of prefix ++ is modified by adding 1. The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type other than cv bool, or a pointer to a completely-defined object type. The result is the updated operand; it is an lvalue, and it is a bit-field if the operand is a bit-field. The expression ++x is equivalent to x+=1.

So ++ can be applied on the result again, because the result is basically just the object being incremented and is an lvalue. In C however:

6.5.3 Unary operators

The operand of the prefix increment or decrement operator shall have atomic, qualified, or unqualified real or pointer type, and shall be a modifiable lvalue.

The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation.

The result is not an lvalue; it's just the pure value of the incrementation. So you can't apply any operator that requires an lvalue on it, including ++.

If you are ever told the C++ and C are superset or subset of each other, know that it is not the case. There are many differences that make that assertion false.

like image 90
StoryTeller - Unslander Monica Avatar answered Oct 27 '22 00:10

StoryTeller - Unslander Monica


In C, it's always been that way. Possibly because pre-incremented ++ can be optimised to a single machine code instruction on many CPUs, including ones from the 1970s which was when the ++ concept developed.

In C++ though there's the symmetry with operator overloading to consider. To match C, the canonical pre-increment ++ would need to return const &, unless you had different behaviour for user-defined and built-in types (which would be a smell). Restricting the return to const & is a contrivance. So the return of ++ gets relaxed from the C rules, at the expense of increased compiler complexity in order to exploit any CPU optimisations for built-in types.

like image 40
Bathsheba Avatar answered Oct 26 '22 23:10

Bathsheba