Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't multiple decrement operators work in C when they work in C++?

People also ask

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 does decrement work in C?

Difference between the Increment and Decrement Operator in C It is used to increment the value of a variable by 1. It is used to decrease the operand values by 1. The increment operator is represented as the double plus (++) symbol. The decrement operator is represented as the double minus (--) symbol.

Can you use ++ in C?

In C/C++, Increment operators are used to increase the value of a variable by 1. This operator is represented by the ++ symbol. The increment operator can either increase the value of the variable by 1 before assigning it to the variable or can increase the value of the variable by 1 after assigning the variable.

What will happen if you use increment and decrement operator on constant?

Increment operator increases the value of the variable by 1. Decrement operator decreases the value of the variable by 1.


In C, --x is a value, not an lvalue. Its effect is to decrement x, and evaluate to the newly assigned value of x. Since --x is not an lvalue, it cannot be decremented.

In C++, --x is an lvalue, not an rvalue. Its effect is to decrement x, and evaluate to x as an lvalue. Since --x is an lvalue again, it can be decremented again.

The reason why it makes sense for --x to be an lvalue in C++ is because C++ introduced reference types. Given

void f(int &);
int i;

it may make sense to call f(--i), which passes i by reference after decrementing it.

Since C doesn't have reference types, there's little point in --i being an lvalue. Historically, it never was, and unlike C++, C never gained a compelling reason to change the rules.

Note that C++ required more extensive changes than making --x an lvalue to actually let it work. Making --x an lvalue, without anything else, would make --x undefined behaviour, because there would not be a sequence point between the modification to x and the subsequent lvalue-to-value conversion. Even more clearly so for ----x. C++ had to modify the sequencing rules to make it work. In C, modifications to the sequencing rules might cause problems for existing compilers to conform to the new rules, so such modifications would likely be rejected unless there's a big benefit.