Compare double value in C
-
19-06-2021 - |
Question
Why is that even if enter value 999999, it will always go to else statement? Can someone explain why and what is the correct way to do this?
#include <stdio.h>
int main(int argc, char **args)
{
double dValue = 0;
scanf("%d",&dValue);
if(10000 < dValue){
printf("More than");
} else {
printf("Less than");
}
return 0;
}
Solution
If you're intending to read in the value as an integer (using "%d"
), then you should declare it to be an int
. If you're intending to read in the value as a double
, then you should instead use "%lf"
as your scanf format specifier.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow