csyntaxnegation

C99 standard: is the logical negation operator defined to also operate directly on a function?


I am curious if there is any difference between assigning the output of a function foo() to a variable and then prefixing the logical negation ! operator vs. prefixing the logical negation operator directly before the function. i.e. is

int output = foo();
if(!output){

}

formally equivalent to:

if(!foo()) {

}

Solution

  • This probably isn't what you're thinking of, but it is a difference between your two example code fragments, and an important one: in

    int output = foo();
    if (!output) {
       ...
    }
    ...
    

    the variable output, and therefore the value returned from the function, is available to code both inside and below the if-statement. In your other example, the value returned from the function is not available to anything but the ! operator inside the if-condition.

    This is probably the most important practical reason to choose one form or the other; do you need that value for anything besides the if-condition? If so, you need the variable.