What is the correct format specifier for double
in printf? Is it %f
or is it %lf
? I believe it's %f
, but I am not sure.
Code sample
#include <stdio.h>
int main()
{
double d = 1.4;
printf("%lf", d); // Is this wrong?
}
What is the correct format specifier for double
in printf? Is it %f
or is it %lf
? I believe it's %f
, but I am not sure.
#include <stdio.h>
int main()
{
double d = 1.4;
printf("%lf", d); // Is this wrong?
}
"%f"
is the (or at least one) correct format for a double. There is no format for afloat
, because if you attempt to pass afloat
toprintf
, it'll be promoted todouble
beforeprintf
receives it1."%lf"
is also acceptable under the current standard -- thel
is specified as having no effect if followed by thef
conversion specifier (among others).Note that this is one place that
printf
format strings differ substantially fromscanf
(andfscanf
, etc.) format strings. For output, you're passing a value, which will be promoted fromfloat
todouble
when passed as a variadic parameter. For input you're passing a pointer, which is not promoted, so you have to tellscanf
whether you want to read afloat
or adouble
, so forscanf
,%f
means you want to read afloat
and%lf
means you want to read adouble
(and, for what it's worth, for along double
, you use%Lf
for eitherprintf
orscanf
).1. C99, §6.5.2.2/6: "If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions." In C++ the wording is somewhat different (e.g., it doesn't use the word "prototype") but the effect is the same: all the variadic parameters undergo default promotions before they're received by the function.
It can be
%f
,%g
or%e
depending on how you want the number to be formatted. See here for more details. Thel
modifier is required inscanf
withdouble
, but not inprintf
.The correct
printf
format fordouble
is%lf
, exactly as you used it. There's nothing wrong with your code.Format
%lf
inprintf
was not supported in old (pre-C99) versions of C language, which created superficial "inconsistency" between format specifiers fordouble
inprintf
andscanf
. That superficial inconsistency has been fixed in C99.So in modern C it makes perfect sense to prefer to use
%f
withfloat
,%lf
withdouble
and%Lf
withlong double
consistently in bothprintf
andscanf
.%Lf
(note the capitalL
) is the format specifier for long doubles.For plain
doubles
, either%e
,%E
,%f
,%g
or%G
will do.For double you can simply use
%lf
or you may use any of following as per your preference%e
or%E
for values in exponential format%g
or%G
for either normal or exponential notation, whichever is more appropriate for its magnitude.Read more at here List of all Format Specifier in C
Given the C99 standard (namely, the N1256 draft), the rules depend on the function kind: fprintf (printf, sprintf, ...) or scanf.
Here are relevant parts extracted:
The same rules specified for
fprintf
apply forprintf
,sprintf
and similar functions.The long story short, for
fprintf
the following specifiers and corresponding types are specified:%f
-> double%Lf
-> long double.and for
fscanf
it is:%f
-> float%lf
-> double%Lf
-> long double.