C语言 c语言中if(!variable_name)是什么意思

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13952423/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:47:55  来源:igfitidea点击:

What does if (! variable_name) mean in c language

c

提问by Ask_it

Here is the problem

这是问题所在

int main() {
    int pid = fork();
    if (!pid) {
        // condition 1
    } else {
        // condition 2
    }
    return 0;
}

What does (!pid)do?

有什么作用(!pid)

回答by md5

It is equivalent to:

它相当于:

if (!pid != 0) /* ... */

And then:

进而:

if (pid == 0) /* ... */

C11 (n1570), § 6.5.3.3 Unary arithmetic operators

The result of the logical negation operator !is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int. The expression !Eis equivalent to (0==E).

C11 (n1570), § 6.5.3.3 一元算术运算符

!如果其操作数的值比较不等于 0,则逻辑否定运算符的结果为 0,如果其操作数的值比较等于 0,则结​​果为 1。结果的类型为int。表达式!E等价于(0==E)

回答by Roman Bodnarchuk

It means negation. In your case condition 1will be executed in a parent process, condition 2in a child process.

意思是否定。在您的情况下,condition 1将在父进程中执行,condition 2在子进程中。

回答by Luca Davanzo

if(!pid)

Is as you wrote:

就像你写的:

if(pid == 0) {
  /* do something */
}

And then:

进而:

if(pid) 

is

if(pid != 0)

回答by Mihai Stancu

In plain old C there is not boolean data type but there is boolean logic.

在普通的旧 C 中,没有布尔数据类型,但有布尔逻辑。

Numeric values all evaluate to trueexcept for 0 which evaluates to false.

true除了 0 的计算结果为之外,所有数值都计算为false

The consequence of this is the fact that if you want to test if a condition is true you are in fact comparing it to 0.

这样做的结果是,如果你想测试一个条件是否为真,你实际上是在将它与 0 进行比较。

Comparison operators in C yield a trueor falseresult meaning they return a numeric 1 or 0.

C 中的比较运算符产生 atruefalse结果,这意味着它们返回数字 1 或 0。

The negation operator inverts trueinto false.

否定运算符反转truefalse

回答by Andrejs Cainikovs

!pidexpression will be true if pidequals to zero.

!pid如果pid等于零,则表达式为真。

Basically, it's the same as:

基本上,它与以下内容相同:

if (pid == 0) {
}

回答by Philipp

The !-operator negates a logical condition.

!-运算符否定逻辑条件。

In C, a numerical value of 0 is considered a logical false, any other numerical value a logical true. The !-operator negates a logical condition, so when pid is 0 it's true and when pid is not 0, it's false.

在 C 中,数值 0 被认为是逻辑假,任何其他数值都被认为是逻辑真。!-运算符否定逻辑条件,因此当 pid 为 0 时为真,当 pid 不为 0 时为假。

You could read it as "when there is no pid".

您可以将其读作“当没有 pid 时”。

回答by Boundless

Things that are false: false, 0, null Things that are true: everything else.

错误的事情:false, 0, null 正确的事情:其他一切。

For if !pid to be true, pid would need to be 0, false, or null

如果 !pid 为真,pid 需要为 0、false 或 null

回答by Klas Lindb?ck

(!pid)is equivalent to (pid == 0)

(!pid)相当于 (pid == 0)

Actually, the code in the question is bugged. fork()can return one of three things:

实际上,问题中的代码被窃听了。fork()可以返回以下三件事之一:

>0The process id of the child process. This is returned to the parent.

>0子进程的进程ID。这将返回给父级。

0This is returned to the child.

0这是返回给孩子的。

<0Failure. This is returned to the parent.

<0失败。这将返回给父级。

Check this tutorialfor the proper use of the return value from fork.

检查本教程以正确使用fork.

回答by anishsane

I guess all the answers posted are correct. In one line, (!pid) would be equivalent to (pid==0).
I however take the opportunity to explain how/why it is used here.

我想所有发布的答案都是正确的。在一行中,(!pid) 等价于 (pid==0)。
然而,我借此机会解释如何/为什么在这里使用它。

fork()function will create a subprocess A.K.A. child process. So when the function returns, at that instance there will be 2 processes, which are executing at the same location code-wise. Thus it will return in two copies of the process - we call them as parent & child.

fork()函数将创建一个子进程又名子进程。因此,当函数返回时,在该实例中将有 2 个进程,它们以代码方式在同一位置执行。因此,它将在进程的两个副本中返回——我们称它们为父和子。

The return value of fork()is the PIDof the child, when it is returning in the parent process. & it is = 0 in child process. In your code, pid = fork();will capture this return value.

的返回值fork()是子进程的PID,当它在父进程中返回时。& 在子进程中为 = 0。在您的代码中,pid = fork();将捕获此返回值。

If (pid==0) then you are in child process. If (pid != 0) you are in parent process. Based on whether you are in parent or child, you can change the behavior of your code. (e.g. you may call exec in child & wait in parent.)

如果 (pid==0) 则您处于子进程中。如果 (pid != 0) 你在父进程中。根据您是父级还是子级,您可以更改代码的行为。(例如,您可以在 child 中调用 exec 并在 parent 中等待。)

For more details about fork function, google for fork().

有关 fork 功能的更多详细信息,请 google for fork().