+ 1
Why does this give me "true"?: int x=-2; if (5/x) cout<<"true"; else cout<<"false";
It's one of the problems in the challenges
4 Respuestas
0
That's the thing. The compiler prints "true". I chose "compiler error" as the answer but i got it wrong
0
it gives a true because the compiler takes the result as an integer and then prints "false" if it's 0 and "true" if it's any other value(because it deals with it as 1)...and here,the integer value of (-5/2) is -2 not equal to 0 so it prints "true"
0
Perfect! That makes a lot more sense! Thank you so much, Abdo17!!
0
@Phumus No, if statements can use int types. It wouldn't make sense to use bool types in it. Since bool wasn't in C, and if if used bool, It wouldn't be in C