-3
#include <stdio.h>

int main(){
    double d;

    scanf("%f", &d);
    printf("%f\n\n",  d);

    system("pause");
    return 0;
}

This is what I get: error.

This code is meant to read the variable double and display it on the screen but it does only display "0.0...".

If I just change the variable type to float it does exactly what I want but if I make it a double it just reads '0'. Why?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • _I'm new to programming and I searched it on the Internet and I couldn't find an answer._, We'll, you certainly did not search enough. Thought I don;t see how google failed to answer your question. – Sourav Ghosh May 17 '16 at 19:52
  • 1
    The basic problem is that when it sees a `%f`, `scanf()` expects it was given a `float *` (and given `%lf` it expects to get a `double *`), and passing a `double *` but telling it to expect a `float *` leads to unhappiness. In some ways, the more interesting question is — Why does this work: `float f = 3.1416; double d = 2.78128; printf("%f %f\n", f, d);`? The answer is part of the standard (it is fully defined behaviour), but is probably more subtle than you need to deal with at the moment. – Jonathan Leffler May 17 '16 at 19:58
  • Oh, and another (jocular) way of looking at the whole problem: You lied to `scanf()`, and it gets its own back by not doing what you want. This is entirely legitimate behaviour on its part. – Jonathan Leffler May 17 '16 at 20:04
  • 1
    A comment on Jonathan Leffler's edit: C is case-sensitive, so `double` and `DOUBLE` are two unrelated names. `double` is a keyword and a type name; `DOUBLE` is just an ordinary identifier. Don't use all-caps for emphasis when discussing C keywords and identifiers. Don't even capitalize them at the beginning of a sentence. – Keith Thompson May 17 '16 at 20:08
  • The compilation of your code, per your link provided, reported "0 warnings". This is key as the problem (mis-match specifier/type) _should_ have been reported. So either 1) insure your complier warning options are fully enabled or 2) use a better compiler. – chux - Reinstate Monica May 17 '16 at 21:19
  • Possible duplicate of [Scanf for double not working in Dev C++](https://stackoverflow.com/questions/27657977/scanf-for-double-not-working-in-dev-c) – phuclv Sep 09 '18 at 04:25

1 Answers1

1

In your code,

 scanf("%f", &d);

is wrong. For scanf(), %f expects a pointer to float as argument.

Quoting C11, chapter §7.21.6.2, fscanf()

a,e,f,g

Matches an optionally signed floating-point number, infinity, or NaN, whose format is the same as expected for the subject sequence of the strtod function. The corresponding argument shall be a pointer to floating.

In case you want to scan a double, you have to use

 scanf("%lf", &d);

FWIW, passing incompatible type of argument for any format specifier invokes undefined behavior.

Quoting C11,

[...] Unless assignment suppression was indicated by a *, the result of the conversion is placed in the object pointed to by the first argument following the format argument that has not already received a conversion result. If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261