1

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

Mostafiz Rahman
  • 8,169
  • 7
  • 57
  • 74
madMDT
  • 448
  • 1
  • 4
  • 16
  • Seems to work: http://ideone.com/Fn7Wzm with a C compiler, http://ideone.com/RomBDW with a C++ compiler. – mah Feb 04 '16 at 18:12
  • 2
    my compiler is compiling it with c++ @harald – madMDT Feb 04 '16 at 18:14
  • 3
    @harald: Which part is not C++? – Benjamin Lindley Feb 04 '16 at 18:14
  • You sure you're not using C89? It was undefined behavior for printf(), specifically in C89, but commonly `%lf` was treated as `%f`. Commonly does not mean always, however. – Zéychin Feb 04 '16 at 18:14
  • @Zéychin I think I am not using C89. Anyway how can I check which compiler is running in codeblocks 10.05? – madMDT Feb 04 '16 at 18:17
  • I used %lf for printing double with printf() before. But I recently started using c++0x and problem happening after that. – madMDT Feb 04 '16 at 18:19
  • and I am unfamiliar with Codeblocks, maybe someone else has some input on this. – Zéychin Feb 04 '16 at 18:23
  • Other than you potentially using the wrong C/C++ standard, my thought is that the compiler is not 100% C++0x/C++11 compliant (Last I checked, no compiler is absolutely 100% compliant yet, but I am not primarily a C++ developer, so I have not checked in some time, so do not quote me on this). – Zéychin Feb 04 '16 at 18:33
  • Possible duplicate of [Correct format specifier for double in printf](http://stackoverflow.com/questions/4264127/correct-format-specifier-for-double-in-printf) – clcto Feb 04 '16 at 18:46
  • disagree that it's a duplicate of that – M.M Feb 05 '16 at 01:14
  • @madMDT your best option is probably to install a more recent compiler. There is also a more recent version of Code::Blocks available. Whatever one you have now is bugged. – M.M Feb 05 '16 at 01:16
  • also see [this answer](http://stackoverflow.com/a/25684134/1505939) although you should upgrade anyway – M.M Feb 05 '16 at 01:19

2 Answers2

1

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.

themantalope
  • 1,040
  • 11
  • 42
  • 1
    I suspect OP is linking against the Windows C library, which has a non-standard (in C99, compliant in C89) `printf` implementation. `%lf` expects a `long double` value there. See https://msdn.microsoft.com/en-us/library/tcxf1dw6.aspx . – Wintermute Feb 04 '16 at 19:11
  • The reference you linked to is wrong. That site contains a lot of mistakes. `%lf` is correct for printing `float` or `double`. – M.M Feb 05 '16 at 01:15
  • @M.M, thanks for letting me know. What references do you use for C++ documentation? – themantalope Feb 05 '16 at 01:21
  • A [C++ standard draft](http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents), or http://www.cppreference.com/ – M.M Feb 05 '16 at 01:24
1

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?)

Community
  • 1
  • 1
Victor Dyachenko
  • 1,363
  • 8
  • 18
  • He's using the Visual Studio compiler in C++ mode; according to MSDN %lf means "long double": http://stackoverflow.com/questions/35208327/c0x-printf-printing-wrong-value-for-double – kfsone Feb 04 '16 at 21:22
  • @kfsone The C++ standard specifies that functions from the C library behave the same in C++ as they would in ISO C99 , unless otherwise noted (and there are no other notes about the printf family) – M.M Feb 05 '16 at 01:13
  • @M.M Exactly. Thank you – Victor Dyachenko Feb 05 '16 at 07:24