Why does this give me "true"?: int x=-2; if (5/x) cout<<"true"; else cout<<"false"; | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

22nd Jul 2016, 11:04 PM
jigoku_tenshi
4 Answers
0
That's the thing. The compiler prints "true". I chose "compiler error" as the answer but i got it wrong
23rd Jul 2016, 1:06 AM
jigoku_tenshi
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"
23rd Jul 2016, 1:25 AM
Abdorrahim tr
Abdorrahim tr - avatar
0
Perfect! That makes a lot more sense! Thank you so much, Abdo17!!
23rd Jul 2016, 1:33 AM
jigoku_tenshi
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
23rd Jul 2016, 1:33 PM
_Geometry dash_ _Roh_ (AKA NovaRate0315)
_Geometry dash_ _Roh_ (AKA NovaRate0315) - avatar