Reputation: 458
Today I was trying to print a double
value with printf
function using %lf
specifier but I got 0.000000
. But when I tried to print the same thing with %f
specifier I got correct output. Why this happened? I use c++0x(c++11 I think.)
#include<stdio.h>
int main()
{
double aaa;
aaa=1.23;
printf("%lf\n",aaa);
printf("%f\n",aaa);
return 0;
}
Upvotes: 1
Views: 1392
Reputation: 1401
From C99:
The conversion specifiers and their meanings are:
f,F - A double argument representing a floating-point number
.
The length modifiers and their meanings are:
l (ell) - ... has no effect on a following a, A, e, E, f, F, g, or G conversion specifier.
So effect must be the same for both calls. (Compiler bug? Old compiler?)
Upvotes: 1
Reputation: 1040
Perhaps this answer isn't totally correct, but according to this reference, it looks like %l
is a length specifier for long int
and %f
is the specifier for float
. Perhaps in the past this worked to print floating point variables, but I would guess that your code is attempting to treat the double variable aaa
as a long int
, and printing 0 as a result.
Upvotes: 1