Why C ++ accepts multiple prefixes but not postfix for variable

When viewing, can you have an increment and decrement in the same variable in the same in c

I found that you can have several increment / decrement prefix operators for one variable, but only one postfix

Example:

++--++foo; // valid
foo++--++; // invalid
--foo++;   // invalid

Why is this?

+5
source share
2 answers

This is due to the fact that in C ++ (but not in C), the result ++xis the value lValue, that is, it can be assigned and, thus, capable of chaining.

However, the result is x++NOT an lValue, instead it is a prValue, that is, it cannot be assigned and therefore cannot be bound.

+8

++ / lvalues, r. , lvalue. , / , lvalue ( /).

++ ,

int i = 0;
int *p = &++i;

i p i. & lvalue-, ++ ( ).

/ , , undefined, , , (.. "" ).

++foo-- , ++ postfix , . . , (++foo)-- , undefined .

+6

All Articles