How is if statement evaluated in c++?

No, if (c) is the same as if (c != 0). And if (!c) is the same as if (c == 0).


I'll break from the pack on this one... "if (c)" is closest to "if (((bool)c) == true)". For integer types, this means "if (c != 0)". As others have pointed out, overloading operator != can cause some strangeness but so can overloading "operator bool()" unless I am mistaken.


If c is a pointer or a numeric value,

if( c )

is equivalent to

if( c != 0 )

If c is a boolean (type bool [only C++]), (edit: or a user-defined type with the overload of the operator bool())

if( c )

is equivalent to

if( c == true )

If c is nor a pointer or a numeric value neither a boolean,

if( c )

will not compile.


It's more like if ( c != 0 )

Of course, != operator can be overloaded so it's not perfectly accurate to say that those are exactly equal.