194

Why is it that scanf() needs the l in "%lf" when reading a double, when printf() can use "%f" regardless of whether its argument is a double or a float?

Example code:

double d;
scanf("%lf", &d);
printf("%f", d);
alk
  • 69,737
  • 10
  • 105
  • 255
raldi
  • 21,344
  • 33
  • 76
  • 86
  • 1
    I don't understand what you mean by POINTER here. In scanf we only pass &variable (i.e)address so where is the pointer –  Jun 17 '12 at 13:04
  • 12
    @deetchanya In C, when you "take the address of" a variable with the unary `&` operator, the result of that operation is a pointer to the variable's storage location in memory. It is that pointer which is passed to `scanf`. – zwol Jun 27 '13 at 23:03
  • this is a another post regarding this https://stackoverflow.com/questions/9291348/why-is-scanf-not-working-as-expected-when-writing-to-a-string-literal – vimalpt Sep 22 '14 at 10:03

5 Answers5

221

Because C will promote floats to doubles for functions that take variable arguments. Pointers aren't promoted to anything, so you should be using %lf, %lg or %le (or %la in C99) to read in doubles.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
MSN
  • 53,214
  • 7
  • 75
  • 105
34

Since С99 the matching between format specifiers and floating-point argument types in C is consistent between printf and scanf. It is

  • %f for float
  • %lf for double
  • %Lf for long double

It just so happens that when arguments of type float are passed as variadic parameters, such arguments are implicitly converted to type double. This is the reason why in printf format specifiers %f and %lf are equivalent and interchangeable. In printf you can "cross-use" %lf with float or %f with double.

But there's no reason to actually do it in practice. Don't use %f to printf arguments of type double. It is a widespread habit born back in C89/90 times, but it is a bad habit. Use %lf in printf for double and keep %f reserved for float arguments.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 5
    I would say that using `%f` in printf is a good habit because then your code always works, whereas using `%lf` may fail if the compiler does not have a C99 compliant library. Unfortunately that situation does happen in reality. – M.M May 22 '16 at 04:39
  • 1
    *Since С99 the matching between format specifiers and floating-point argument types in C is consistent between `printf` and `scanf`.* Note that this does not imply that using the same format specifier means that the data written by a `[f]printf()` can be read by `[f]scanf()`. In general, using the same format specifier for `scanf()` that was used by `printf()` will **not** successfully read the data. For example, space padding that can be inserted by a `prinf()`'s `"%d"` format specifier will be skipped by that same `"%d"` format specifier in a `scanf()` call. – Andrew Henle Jul 14 '18 at 18:02
17

scanf needs to know the size of the data being pointed at by &d to fill it properly, whereas variadic functions promote floats to doubles (not entirely sure why), so printf is always getting a double.

DigviJay Patil
  • 986
  • 14
  • 31
Tanktalus
  • 21,664
  • 5
  • 41
  • 68
  • 1
    A variadic function is very fragile, because it needs to be able to know the exact type and size of all the parameters passed to it, and it can't enforce this at compile time. If a variable is the wrong type, the wrong value will be read; if it's the wrong size, all the variables after will be misread too. If two different sizes of float could be passed, then it would cause all kinds of nasty and easy-to-miss problems. – mwfearnley Nov 05 '16 at 12:54
7

Because otherwise scanf will think you are passing a pointer to a float which is a smaller size than a double, and it will return an incorrect value.

Jim Buck
  • 20,482
  • 11
  • 57
  • 74
3

Using either a float or a double value in a C expression will result in a value that is a double anyway, so printf can't tell the difference. Whereas a pointer to a double has to be explicitly signalled to scanf as distinct from a pointer to float, because what the pointer points to is what matters.

fcw
  • 2,274
  • 1
  • 14
  • 7
  • 5
    float is converted to a double *in this case* because the arguments are part of a variable-length argument list, floats are not always converted to doubles in C. – Robert Gamble Oct 16 '08 at 23:19
  • 1
    In pre-standard versions of C language `float` values were automatically promoted to `double` in expressions. That rule was abandoned in standard C. Generally, `float` does not get promoted to `double` in expressions. It only gets promoted to `double` when passed as a variadic argument, which is what happens in this case. – AnT stands with Russia Jan 29 '15 at 19:00