Question

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";
}
Was it helpful?

Solution

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.

OTHER TIPS

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";
}

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

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";
}

Because true is 1. If you want to test a for a non-zero value, just write if(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.

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";
}

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).

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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top