Can someone pls explain me why this is not printing hello?
int main(){
float i;
i=1.2;
while (i==1.2){
printf("hello");
}
Can someone pls explain me why this is not printing hello?
int main(){
float i;
i=1.2;
while (i==1.2){
printf("hello");
}
Computers only store digital information. Integers can be represented accurately in binary, but floating point numbers are approximated.
It seems that in the approximations, additional tiny values are preventing your exact comparison from being true.
Now is a good time to google "what every programmer should know about floating point numbers" and read it. It will save you countless hours of future programming and debugging if you learn it early.
In addition, there are two floating point types "float" and "double". You are comparing a float to a double, so the approximations of the value are likely not the same approximation, creating more of a chance that the two values won't be equal.