Here is the problem
int main() {
int pid = fork();
if (!pid) {
// condition 1
} else {
// condition 2
}
return 0;
}
What does (!pid)
do?
The way to understand the “!” operator is to think that as a “not”, so when you ask if(! programming) is a way to express “if it does not program do such”…. Submitted by Mario Bianchi.
The if statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false. One of the important functions of the if statement is that it allows the program to select an action based upon the user's input.
Definition and Usage In JavaScript we have the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false.
:= is not a valid operator in C. It does however have use in other languages, for example ALGOL 68. Basically, for what you want to know, the := in this example is used to assign the variable PTW32_TRUE to localPty->wNodeptr->spin.
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 typeint
. The expression!E
is equivalent to(0==E)
.
if(!pid)
Is as you wrote:
if(pid == 0) {
/* do something */
}
And then:
if(pid)
is
if(pid != 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With