Feeling confused with -(--a) vs --(-a) in c

微笑、不失礼 提交于 2019-12-19 10:08:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!