41

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

derrdji
  • 12,661
  • 21
  • 68
  • 78
  • 2
    Bear in mind that 0 doesn't always mean the same thing. It can be the null pointer constant or integer zero. – David Thornley Sep 25 '09 at 19:37
  • 6
    Don't you mean "Is if ( c ) the same as if ( c != 0 ) in C++?" ? – Peter Mortensen Sep 25 '09 at 21:10
  • 1
    Is the variable c of a particular type? – Peter Mortensen Sep 25 '09 at 21:15
  • Just int. But what happens if c is a pointer? 'if (c)' checks if c is pointing to something? – derrdji Sep 25 '09 at 21:22
  • 3
    @derrdji, it's comparing against 0, which is value zero of type int. Depending on the type of c, different things will happen. If c is an object of a class, its overloaded operator== can be called. If c is a double, then the integer will be converted to double. And if c is a pointer, then the integer will be converted to a null pointer, and the comparison against the null pointer will check whether `c` points to a valid object (or function) of its type. Note that in all cases, 0 is always the same and always means the same. It just can be converted to many different things. – Johannes Schaub - litb Sep 25 '09 at 23:30
  • Possible duplicate of [Do negative numbers return false in C/C++?](http://stackoverflow.com/questions/18840422/do-negative-numbers-return-false-in-c-c) – Archmede May 01 '17 at 02:05

7 Answers7

77

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

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • 10
    Assuming no odd overloading is taking place, which is often part of the answer in C++. – David Thornley Sep 25 '09 at 19:38
  • 6
    Ofcourse, if the operators == or != are overloaded, anything could happen, it could even start playing the national anthem for you... ;-) – Jesper Sep 26 '09 at 06:27
  • 9
    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 Nov 05 '09 at 05:59
25

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
  • 58,213
  • 10
  • 98
  • 113
7

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
  • 14,136
  • 6
  • 46
  • 59
3

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
  • 414,610
  • 91
  • 852
  • 789
0

Yes they are the same if you change == 0 to != 0.

Winter
  • 3,894
  • 7
  • 24
  • 56
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
0

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
  • 17,802
  • 19
  • 72
  • 101
-5

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
  • 13,334
  • 2
  • 38
  • 57
  • no, it's not... both mean absolutely the same in the case of a pointer: if( c != NULL ) – Massa Sep 25 '09 at 22:38
  • 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 Sep 26 '09 at 04:47
  • @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 Sep 26 '09 at 08:06
  • 1
    @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 Sep 26 '09 at 08:09
  • 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 Sep 26 '09 at 11:10