views:

480

answers:

7

Is if ( c ) the same as if ( c == 0 ) in C++?

+3  A: 

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.

Mehrdad Afshari
+10  A: 

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

Jesper
Assuming no odd overloading is taking place, which is often part of the answer in C++.
David Thornley
Ofcourse, if the operators == or != are overloaded, anything could happen, it could even start playing the national anthem for you... ;-)
Jesper
One of these days I'm going to make a class which overloads operators to play sound effects. It's gonna be awesome every time someone wants to sort them.
rlbond
A: 

yes they are the same if you change

== 0

to

!= 0

Brian R. Bondy
Uh, no they're not.
Aistina
The if statement is *defined* to accept anything non-zero as true. So yes it is the same.
Zan Lynx
+9  A: 

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.

D.Shawley
The question was originally tagged with only C, hence the above answers since there is no bool in C.
Brian R. Bondy
if(static_cast<bool>(c)) ... I'll get my coat.
alex tingle
C99 has Bool...
rlbond
Or `if(bool cond = c) ...;` to show the conversion is implicit.
Johannes Schaub - litb
A: 

This is only true for numeric values. if c is class, there must be an operator overloaded which does conversion boolean, such as in here:

#include <stdio.h>

class c_type
{
public:
 operator bool()
 {
  return true;
 }
};

int main()
{
 c_type c;
 if (c) printf("true");
 if (!c) printf ("false");
}
galets
+1  A: 

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.

Patrice Bernassola
It c is a user-defined type one can just implement `operator bool()` as D. Shawley and galets both pointed out and so it will compile just fine.
Troubadour
Exact, I add it to the explanation
Patrice Bernassola
A: 

If c is a pointer then the test

if ( c )

is not quite the same as

if ( c != 0 )

The latter is a straightforward check of c against the 0 (null) pointer whereas the former is actually a instruction to check whether c is points to a valid object. Typically compilers produce the same code though.

Troubadour
no, it's not... both mean absolutely the same in the case of a pointer: if( c != NULL )
Massa
There is no way for the compile and/or the runtime to know whether an object is valid in either C or C++.You can write:" X* c = (X*)1; "Then c almost certainly points to an invalid object. And" if(c) " will be true.
Andrew Stein
@Andrew: I doubt you can justify your statement that there is no way to check that the object is valid or not. Certainly it would require a lot of effort by the compiler to generate run-time code to check and that's why compilers don't bother and generate the same code i.e. test against the null pointer. That's why your example fails to produce a false result on (probably) all current compilers.
Troubadour
@Massa: I'm afraid not. My reference is section 6.3.2 (Selection Statements) from "The C++ Programming Language, Special Edition" by Bjarne Stroustrup (the creator of C++). What's yours?
Troubadour
Comparing an object pointer against a null pointer does check whether it points to a valid object, while "valid object" means "object with non-null-pointer address"
Johannes Schaub - litb