madMDT
madMDT

Reputation: 458

C++0x printf() printing wrong value for double

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;
}

enter image description here

Upvotes: 1

Views: 1392

Answers (2)

Victor Dyachenko
Victor Dyachenko

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

themantalope
themantalope

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

Related Questions