C语言 使用感叹号 '!' 在 C
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22855483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Exclamation Marks '!' in C
提问by Paul Filch
I have come across a problem involving exclamation marks and integers whilst reading a code in my reference book.
我在阅读参考书中的代码时遇到了一个涉及感叹号和整数的问题。
Let us say I have declared an integer variable named number - int number = 0;
假设我已经声明了一个名为 number 的整数变量 - int number = 0;
I then use a while function involving an exclamation mark and number
然后我使用一个包含感叹号的 while 函数和 number
while(!number)
{
...
}
I am confused with this because I do not know what does !numbermean and what would be possible returned results? I am not sure if this can be used, but as I said, I saw it in my book.
我对此感到困惑,因为我不知道这!number意味着什么以及可能返回的结果是什么?我不确定这是否可以使用,但正如我所说,我在我的书中看到了它。
Therefore, it would be great if someone could tell me what does !numbermean and what does it evaluate?
因此,如果有人能告诉我什么意思以及它评估什么,那就太好了?!number
Thank you in advance.
先感谢您。
回答by gangadhars
We can treat !as not.
So if a number is non-zero (either positive or negative) it returns Zero.
If it is zero, it returns 1.
我们可以视而不见!。因此,如果数字非零(正数或负数),则返回零。如果为零,则返回 1。
int i = 13;
printf("i = %d, !i = %d\n", i, !i);
printf("!0 = %d\n", !(0));
回答by TheWalkingCube
In C, !number will evaluate to 1 if number == 0 and to 0 if number != 0. And in C, 1 is true and 0 is false.
在 C 中,!number 在 number == 0 时为 1,在 number != 0 时为 0。在 C 中,1 为真,0 为假。
Using an explicit comparison like number == 0 have the same effect but you might find it easier to read.
使用像 number == 0 这样的显式比较具有相同的效果,但您可能会发现它更易于阅读。
回答by RobP
It's a negation or "not" operator. In practice !number means "true if number == 0, false otherwise." Google "unary operators" to learn more.
这是一个否定或“非”运算符。实际上 !number 的意思是“如果 number == 0 则为真,否则为假。” 谷歌“一元运算符”以了解更多信息。
回答by Umesh
It is used for Negation of a number.It is a Unary Operator.
它用于数字的否定。它是一元运算符。
For Example:-
例如:-
If we are using it with zero :- !0 then it will become 1
如果我们使用它为零 :- !0 那么它将变成 1
with one !1 = 0
一个 !1 = 0
回答by Sorcrer
The negation operator(!) simply just reverses the meaning of its operand.
该反运算符(!)只是简单地反转其数的含义。
The operand or the expression must be of arithmetic or pointer type. But the operand/result of expression is implicitly converted to data type bool (boolean 0 means false, Non zero means True).
操作数或表达式必须是算术或指针类型。但是表达式的操作数/结果被隐式转换为数据类型 bool(布尔值 0 表示假,非零表示真)。
The result is true if the converted operand is false; the result is false if the converted operand is true. The result is of type bool.
如果转换后的操作数为假,则结果为真;如果转换后的操作数为真,则结果为假。结果是 bool 类型。
so
所以
while(!number)
{
...
}
since variable number is 0 , while(!number) ie, !0 which is 'negation of 0' which is 'TRUE' then it code enters the while loop()
由于变量编号为 0 ,而 while(!number) 即 !0 是“0 的否定”,即“真”,然后它的代码进入 while 循环()

