Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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";
}
like image 545
yesraaj Avatar asked Sep 29 '08 12:09

yesraaj


People also ask

Is return 0 true?

return 0 means that the user-defined function is returning false.

Can we return true in C?

true and false are C++. C doesn't have them.

What value is true in C?

C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.


1 Answers

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.

like image 140
paradoja Avatar answered Oct 16 '22 02:10

paradoja