someguy
someguy

Reputation: 453

! meaning in c++ (following context)

I know what != is when if(x!=value){...}, but what does it mean in the following context:

 if (! ReadConsoleInput(
                hStdin,      // input buffer handle
                irInBuf,     // buffer to read into
                128,         // size of read buffer
                &cNumRead) ) // number of records read
            ErrorExit("ReadConsoleInput");

or

if (! SetConsoleMode(hStdin, fdwMode) )
        ErrorExit("SetConsoleMode");

or

 if (! GetConsoleMode(hStdin, &fdwSaveOldMode) )
        ErrorExit("GetConsoleMode");

Upvotes: 2

Views: 463

Answers (3)

littleadv
littleadv

Reputation: 20272

! is LOGICAL NOT, i.e.: if (! boolVar) equals to if (true != boolVar), and if (! intVar) equals to if (0 == intVar)

If you have a function foo() that returns 0 on error, checking if (! foo()) is basically checking if the function succeeded or not, enter brackets on failure.

You have, of course, to know exactly the return values policy for each function, there's no law or rule about it.

Upvotes: 3

Tony Delroy
Tony Delroy

Reputation: 106116

! means "logical-not"... it inverts the boolean sense of the following value (i.e. tests the following value is false). If necessary, the following value will be converted to a boolean first: numeric/pointer values other than 0 are true, 0 (NULL) is false; classes can provide a conversion operator that will provide either a bool or a numeric/pointer type convertible to bool. Standard-compliant compilers even let you write the functionally identical code:

if (not xyz...)

Upvotes: 0

user541686
user541686

Reputation: 210485

It's the "Not" operator: true (1) if the operand is zero, false (0) otherwise.

Upvotes: 8

Related Questions