问题
Confusion with ++ and -- operator
int a = 10;
printf("%d\n", -(--a) ); // valid
output: -9
But, problem occurs when following is used:
printf("%d\n", --(-a)); // error, invalid
Why?
回答1:
The ++
and --
operator works on only lvalue, not value. An lvalue is something that can stand on the left side of an assignment.
printf("%d\n", -(--a) );
Here, --
operator works on variable a
, so this is valid.
But,
printf("%d\n", --(-a));
Here, (-a)
returns a value. --
is applied to a value, which is not valid.
This is because --
modifies a variable, and int
value can't be modified (For example you can't do 7 = 5
but you can do a = 5
)
来源:https://stackoverflow.com/questions/27723802/feeling-confused-with-a-vs-a-in-c