0

When I put 1 for example this return 1.000 (that's fine to me):

#include <stdio.h>

main()
{
    float num;

    printf("Double: ");
    scanf("%f", &num);

    printf("%f\n", num);
}

So, when I put 1 at this it returns 0.000. I don't understand because I used %f to read and write correctly. Can you guys explain me?

#include <stdio.h>

main()
{
    double num;

    printf("Double: ");
    scanf("%f", &num);

    printf("%f\n", num);
}
Ricardo
  • 67
  • 8
  • 1
    Use `scanf("%lf", ...)` for `double`s – David Ranieri Jun 22 '16 at 15:10
  • 1
    `%f` in `printf()` prints a `double`, and `float` is promoted to `double` when passed to it. However, `%f` for `scanf()` is for scanning `float`, and doesn't normally work for `double`... try `%lf` – Dmitri Jun 22 '16 at 15:11
  • My book doesn't show this, but still is a good book for beginners in C. Thanks you guys and I'm sorry for the duplicated question. – Ricardo Jun 22 '16 at 15:24

0 Answers0