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";
}
return 0 means that the user-defined function is returning false.
true and false are C++. C doesn't have them.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With