views:

1032

answers:

11

Why does the code below return true only for a = 1?

main(){
int a = 10;
if (true == a)
     cout<<"Why am I not getting executed";
}
+1  A: 

Because true is 1. If you want to test a for a non-zero value, just write if(a).

OregonGhost
Eek. If you want to test for a non-zero value, write if(a!=0). 3 extra characters to say what you're actually thinking, instead of saying something that the language makes equivalent to what you're thinking. Bargain.
Steve Jessop
It's 5 extra characters, because I would add spaces between the operator and its operands ;)
OregonGhost
Oh, well, in that case point taken ;) I used to use if(a) a lot for pointers, meaning sort of "if there is an a". But eventually I decided that for me it wasn't really helping, and gave it up.
Steve Jessop
I don't use it anymore as well, since I'm writing most code in C# today, where int cannot be converted to bool implicitly. C++ even encourages this style, e.g. with fstream, where you test like if (!myfile) {} etc.May be a good recommendation not to test numbers like this though.
OregonGhost
+5  A: 

Your boolean is promoted to an integer, and becomes 1.

daveb
A: 

I wouldn't expect that code to be defined and you shouldn't depend on whatever behavior your compiler is giving you. Probably the true is being converted to an int (1), and a is not being converted to a bool (true) as you expect. Better to write what you mean (a != 0) then to depend on this (even if it turns out to be defined).

Lou Franco
A: 

something different from 0 (that is false) is not necessary true (that is 1)

Enreeco
A: 

Because a boolean is a bit in C/C++ and true is represented by 1, false by 0.

Update: as said in the comment my original Answer is false. So bypass it.

boutta
C/C++ do not prescribe boolean to be a bit. In C++ bool size is implementation dependent. C has no bool type - it's really an integer.
Steve Fallows
C99 has a bool type, of sorts, enabled by #include <stdbool.h>. But I agree that the bool type is not a bit in either C or C++.
Jonathan Leffler
Thanks for the correction - haven't looked a pure C in a while.
Steve Fallows
A: 

Because true is equal to 1. It is defined in a pre-proccesor directive, so all code with true in it is turnbed into 1 before compile time.

Charles Graham
Tha is just wrong ! true and false are symbols defined in the language and have nothing to do with the preprocessor (unlike in old C with TRUE and false) !
PierreBdR
+34  A: 

When a Bool true is converted to an int, it's always converted to 1. Your code is thus, equivalent to:

main(){
   int a = 10;
   if (1 == a)
      cout<<"y i am not getting executed";
   }

This is part of the C++ standard, so it's something you would expect to happen with every C++ standards compliant compiler.

paradoja
I think you mean "it's always converted to 1" instead of "it's always converted to true"
therefromhere
Oops, you're right, thanks! :) (Edited)
paradoja
Maybe a quick explanation of why it's the bool converted to int, not the int converted to bool? I mean, "because the C++ standard says so" covers most of it, but still...
Steve Jessop
The value of "true" when converted to int is compiler specific, or is that part of the standard?
David Hill
It's part of the standard (edited to include it and a link to the where it says so in the standard).
paradoja
the author of the original code was probably thinking: if( true == bool(a) ), which will convert 10 to true. ie, bool(5) != 5.
Aaron
+26  A: 

The reason your print statement is not getting executed is because your boolean is getting implicitly converted to a number instead of the other way around. I.e. your if statement is equivalent to this: if (1 == a)

You could get around this by first explicitly converting it to a boolean:

main(){
int a = 10;
if (((bool)a) == true)
     cout<<"I am definitely getting executed";
}

In C/C++ false is represented as 0.

Everything else is represented as non zero. That is sometimes 1, sometimes anything else. So you should never test for equality (==) to something that is true.

Instead you should test for equality to something that is false. Since false has only 1 valid value.

Here we are testing for all non false values, any of them is fine:

main(){
int a = 10;
if (a)
     cout<<"I am definitely getting executed";
}

And one third example just to prove that it is safe to compare any integer that is considered false to a false (which is only 0):

main(){
int a = 0;
if (0 == false)
     cout<<"I am definitely getting executed";
}
Brian R. Bondy
use function-style conversion or a C++ cast. C-style cases are evil. 'bool(a)' or 'static_cast<bool>(a)'.
Aaron
Aaron, sorry that is nonsense :) "function-style cast" is semantically equivalent to a (T)x style cast, and it introduces ambiguities in addition. If anything, it's a matter of taste, but T(x) arguably has more problems than (T)x.
Johannes Schaub - litb
+3  A: 

in C and C++, 0 is false and anything but zero is true:

if ( 0 ) { // never run }

if ( 1 ) { // always run }

if ( var1 == 1 ) { // run when var1 is "1" }

When compiler calculates a boolean expression it is obliged to produce 0 or 1. Also, there's a couple handy typedefs and defines, which allow you to use "true" and "false" instead of 1 and 0 in your expressions.

So your code actually looks like this:

main(){ int a = 10; if (1 == a) cout<<"y i am not getting executed"; }

You probably want:

main(){ int a = 10; if (true == (bool)a) cout<<"if you want to explicitly use true/false"; }

or really just:

main(){ int a = 10; if ( a ) cout<<"usual C++ style"; }

n-alexander
+1  A: 

I suggest you switch to a compiler that warns you about this... (VC++ yields this: warning C4806: '==' : unsafe operation: no value of type 'bool' promoted to type 'int' can equal the given constant; I don't have another compiler at hand.)

I agree with Lou Franco - you want to know if a variable is bigger than zero (or unequal to it), test for that.

Everything that's done implicitly by the compiler is hazardous if you don't know the last detail.

xtofl
A: 

Here is the way most people write that kind of code:

main(){
int a = 10;
if (a) // all non-zero satisfy 'truth'
     cout<<"y i am not getting executed";
}

I have also seen:

main(){
int a = 10;
if (!!a == true) // ! result is guaranteed to be == true or == false
     cout<<"y i am not getting executed";
}
Cade Roux