Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does if (! variable_name) mean in c language [closed]

Tags:

c

Here is the problem

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

What does (!pid) do?

like image 312
Ask_it Avatar asked Dec 19 '12 12:12

Ask_it


People also ask

What is meant by if (! Condition?

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.

What is if variable in C?

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.

What does if (!) Mean in JavaScript?

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.

What does := mean in C?

:= 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.


2 Answers

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 !E is equivalent to (0==E).

like image 119
md5 Avatar answered Oct 26 '22 19:10

md5


if(!pid)

Is as you wrote:

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

And then:

if(pid) 

is

if(pid != 0)
like image 35
Luca Davanzo Avatar answered Oct 26 '22 17:10

Luca Davanzo