4

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;
}
Azuan
  • 878
  • 2
  • 13
  • 33
  • Try `%f` or `%g` in the `scanf` call. – vpit3833 Jul 09 '12 at 05:54
  • 4
    `%d` -> `%lf`, Turn up your compiler warnings. – Mysticial Jul 09 '12 at 05:54
  • `%d` does *not* mean `double`. It means decimal conversion to `int`. – wallyk Jul 09 '12 at 05:57
  • @wallyk thanks. changed to %lf, and it works fine. but I print the value of dValue, its still printing the 999999 value. why is it still wrong when I try to compare it in the if statement, which is comparing 999999 with another int value? – Azuan Jul 09 '12 at 06:03
  • @Azuan: You enter 99999 or 999999 and the program prints "less than"? Is that what you are saying? – wallyk Jul 09 '12 at 06:07
  • @wallyk lets say I use the same code as above. then I enter 999999. why does it still skipping the if statement? shouldn't 10000 < 999999 should be true? – Azuan Jul 09 '12 at 06:13
  • @Azuan: Yes it should. So it should print `More than`. What does it actually do? – wallyk Jul 09 '12 at 06:15
  • @wallyk weird. it seems it will always print Less than. – Azuan Jul 09 '12 at 06:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13593/discussion-between-wallyk-and-azuan) – wallyk Jul 09 '12 at 06:18

1 Answers1

7

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.

reuben
  • 3,360
  • 23
  • 28
  • changed to %lf, and it works fine. but I print the value of dValue, its still printing the 999999 value. why is it still wrong when I try to compare it in the if statement, which is comparing 999999 with another int value? – Azuan Jul 09 '12 at 06:01
  • @Azuan How are you trying to print `dValue`? – reuben Jul 09 '12 at 06:03
  • I use %d to print the value, since I use %d to scanf. lets say I use the same code as above. then I enter 999999. why does it still skipping the if statement? shouldn't 10000 < 999999 should be true? – Azuan Jul 09 '12 at 06:16
  • @Azuan To be clear, you're saying that you've fixed the bug, declared `dValue` as an `int`, use `"%d"` with `scanf`, input a number larger than 10000, and you're seeing the `else` branch of the `if` condition executed? – reuben Jul 09 '12 at 06:20
  • not actually. I use the same code as above, which mean I didn't fix the bug yet. then I input number lager than 10000, but it still go to the 'else' statement – Azuan Jul 09 '12 at 06:23
  • @Azuan Fix the bug. The bug is causing the value to get garbled. – reuben Jul 09 '12 at 06:24
  • I think you are entering a number which is greater than maximum value of int. So compiler calculates its equivalent and it turns out to be less than 10000. Suppose you are entering 20000 and you maximum allowed integer value is 19000(assume). Then 20000 will be treated as -1000. And -1000 is less than 10000. The size of integer depends on the compiler. Please check the maximum value of integer for your compiler. – Narendra Jul 09 '12 at 07:38