0
Why does my program hv no output
#include <stdio.h> #include <stdlib.h> int main() { float x = 1.1; while(x == 1.1) { printf("x = %f",x); x = x - 0.1; } return 0; } https://code.sololearn.com/ckongjTmvl2r/?ref=app
14 odpowiedzi
+ 4
Zane Vijay Falcao
Yes
You can read answer here about why both are not equal
https://www.quora.com/What-is-the-difference-between-float-and-double-in-C-language-How-is-it-stored-in-the-memory?top_ans=14244454
+ 4
Zane Vijay Falcao
If you use double instead of float then condition will be true
double x = 1.1;
if (x == 1.1) //true
float y = 1.1;
if (y == 1.1) //false
+ 3
Zane Vijay Falcao
Both x = 1.1 and 1.1 seems equal but that's not
So do this:
while (x > 1.1)
Or do this:
while(x == (float) 1.1)
+ 3
Thanks
+ 1
It seems you have an infinite loop, then execution gets aborted. Pls link your code from Code Playground into the question (edit the question) for us to see the exact response.
+ 1
Done Emerson Prado
+ 1
It is really an issue of precision.
Pls read here a good reference on comparing float values:
https://www.tutorialspoint.com/what-is-the-most-effective-way-for-float-and-double-comparison-in-c-cplusplus
0
The question is to predict the output n explain Y don't get a output
0
The value of test condition is true so it should execute the printf statement
0
(float) means type casting right??
0
Erm no idea
0
try this:
printf("%d\n", x==1.1)
x==1.1 is 0
while(0) will never get executed.
- 1
trt while(x<=1.1)
- 1
it may be because you used == symbol inside the while