我不明白这背后的逻辑..
我希望这个程序输出 2
但它输出 3
您能解释一下以下代码的工作原理吗:
#include <stdio.h>
int main()
{ int a = - -3;
printf("a=%d", a);
return 0;
}
最佳答案
在
int a = - -3;
这条语句没有--(decrement operator)
,是unary minus运算符,使得- -3
为3(取消否定)
。因此它打印 3
。
旁注,如果你想到这一点
int a = --3;
as prints 2
然后你认为错了,因为这会导致 lvalue error 因为 --
适用于变量而不是常量。正确的是
int a = 3;
--a ;/* this is valid,this make a as 2 now */
关于c - 程序输出没有意义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52219735/