感觉混淆 - ( - A)VS - ( - A)在C [英] Feeling confused with -(--a) vs --(-a) in c

查看:166
本文介绍了感觉混淆 - ( - A)VS - ( - A)在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

混淆 ++ - 运营商

int a = 10;
printf("%d\n", -(--a) ); // valid

output: -9 

使用以下时

但是,出现问题:

But, problem occurs when following is used:

printf("%d\n", --(-a));  // error, invalid

为什么?

推荐答案

++ - 运营商适用于只有左值,而不是价值。左值的东西,可以站在一个赋值的左侧。

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) );

在这里, - 操作符对变量 A ,所以这是有效的。

Here, -- operator works on variable a, so this is valid.

但是,

printf("%d\n", --(-a));

在这里,( - A)返回一个值。 - 应用于价值,这是无效的。
这是因为 - 修改一个变量, INT 价值不能被修改(比如你可以不做 7 = 5 但你可以做 A = 5

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)

这篇关于感觉混淆 - ( - A)VS - ( - A)在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆