0

I've made extremely easy program:

#include <stdio.h>
int main()
{
    double x;
    printf("Write your number \n");
    scanf ("%f", &x);
    printf("You've written %f \n", x);

    return 0;
}

And as a result strange number appears (no matter what x I give): "You've written 83096261053132580000000000000000000000000000000000000000000"

What's wrong with this? This program works fine when I change all numbers into an 'int' type.

msc
  • 33,420
  • 29
  • 119
  • 214
Antoni Bąk
  • 51
  • 1
  • 5

4 Answers4

1

See what happens when you compile it with warnings enabled:

amb@nimrod-ubuntu:~/so$ gcc -Wall x.c -o x
x.c: In function ‘main’:
x.c:6:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat]

Change %f to %lf and the scanf and printf functions will correctly take a double.

abligh
  • 24,573
  • 4
  • 47
  • 84
1

Wrong specifier for scanf().

With scanf(), "%f" matches a float *, yet coded passed a double *. Use "%lf" instead. Code's printf() is fine. See Correct format specifier for double in printf

 double x;
 printf("Write your number \n");
 // scanf ("%f", &x);
 scanf ("%lf", &x);
 printf("You've written %f \n", x);

A good compiler should have warned of your incorrect code as suggested by @abligh. Either enable all warnings or consider a new compiler.


When scanning, the &x must match the proper scanf() print specifier.

  • "%f" matches a float *
  • "%lf" matches a double *

The using printf() things are easier. If a float or double is passed, being a variadic function, float is promoted to double.

  • "%f" and "%lf" match a double
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    `%lf` only prints a double since C99; if you work in environments where you might not have a C99-compliant C library then using `%f` for printing increases your "portability" and there's no down-side. – M.M Oct 26 '14 at 23:19
  • @Matt McNabb Agreed. Note: A good use of `"%lf"` for `printf()` is to use the the same format string as `scanf()` in select cases. There is a risk of portability to compilers that are 15+ years out of date. – chux - Reinstate Monica Oct 26 '14 at 23:27
  • MSVC still didn't support `%zu` as of 2011 at least (although they do have `%lf`) – M.M Oct 26 '14 at 23:30
0

%f is used for float type. You should use %lf for double (Long Float).

Vladyslav
  • 786
  • 4
  • 19
  • In `scanf()` `"%f"` is matched with a `float *`, not `float`. In `printf()` `"%f"` is match with a `double`, not `float`. A `float` passed to variadic function is converted to `double`, thus `printf()` will not receive a `float`. – chux - Reinstate Monica Oct 26 '14 at 23:04
0

You need to use %lf for scanf and printf.

In other words, this:

#include <stdio.h>
int main()
{
    double x;
    printf("Write your number \n");
    scanf ("%lf", &x);
    printf("You've written %lf \n", x);

    return 0;
}
MightyMouse
  • 13,208
  • 8
  • 33
  • 43
  • Using `"%f"` is just fine for OP as part of `double x; ... printf("You've written %f \n", x);`. OP's problem is only with `scanf()`. – chux - Reinstate Monica Oct 26 '14 at 23:06
  • Apparently you can not read between the lines, because the OP did not know that doubles require `lf` instead of just `f` when one wants to treat the variables as doubles. – MightyMouse Oct 26 '14 at 23:09
  • "doubles require lf instead of just f" is incorrect. With `scanf()`, `"%f"` works with `float*` and `"%lf"` works with `double*`. This answer assets `%lf` is needed with `printf()` which is incorrect. With `printf()`, either `"%f"` and `"%lf"` work. – chux - Reinstate Monica Oct 26 '14 at 23:13
  • 1
    @MightyMouse apparently you are not aware than `%f` is correct for using `double` with `printf` :) – M.M Oct 26 '14 at 23:16
  • While it is true that both work, it is much easier as a mnemonic rule, since doubles are like "two floats" (hence doubles), or in other words, long doubles. And besides, when people are using `%f` for doubles with `printf`, this only shows sloppiness. Hope you get it at some point. This guy is learning the language. – MightyMouse Oct 26 '14 at 23:25
  • Had this answer suggest "easier as a mnemonic rule" rather than "need to use %lf ... printf.", it would be a reasonable opinion rather than an inaccuracy. Bringing "in other words, long doubles" further confuses the issue as `long doubles` is a 3rd type of floating point distinct from `float` and `double`. Using `"%f"` is not necessarily a sign of sloppiness as various compilers, even in 2014, do not yet accept this C99 feature. If you're stuck with a C89 library, "%lf" is undefined. – chux - Reinstate Monica Oct 26 '14 at 23:38
  • Of course `long doubles` was my mistake and I meant `long floats` (but SO did not let me edit the comment when I saw that and then I thought it was much better to see if you were going to comment on that; you did not disappoint me at all; you proved what you are made of). Moreover, you can also work with Eniac if you want to do so. I will not argue further with you. – MightyMouse Oct 26 '14 at 23:42
  • Rather than commenting on users, recommend sticking to the content of the questions, answer and comments. [Be nice](http://stackoverflow.com/help/be-nice) – chux - Reinstate Monica Oct 26 '14 at 23:47