PraveenDhinwa's blog

By PraveenDhinwa, 11 years ago, In English
#include <bits/stdc++.h>

using namespace std;

int main() {
      double d = 50;
      printf ("%lf\n", d);

      return 0;
}

If I compile it using g++ A.cpp -std=c++0x, output is 0.000000.

If I compile it using g++ A.cpp -std=gnu++0x, output is 50.000000 as desired.

Gives correct answer for using cout in both the cases.

What could be the possible issues? My g++ version is (tdm-1) 4.7.1

EDIT: Based on suggestion of andreyv, I checked using %f, it works correctly. But I dont know the reason for this :(

  • Vote: I like it
  • +12
  • Vote: I do not like it

»
11 years ago, # |
Rev. 2   Vote: I like it +6 Vote: I do not like it

In C++98/C++03 the correct format to print a double is %f. C++11 adopts a change from C99, where the l length modifier was made no-op for the f specifier, so that %lf can also be used.

EDIT: I read your first line as -std=c++98. Still the above facts are most likely related to the cause.

  • »
    »
    11 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Using %f works in both correctly. But I still can not find reason of %lf not working in c++0x.

    • »
      »
      »
      11 years ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      Probably the C++11 support in the compiler/standard library is just incomplete. You could generate preprocessed sources in both cases and see whether two different printf versions are used.

      • »
        »
        »
        »
        11 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        First one uses ___mingw_vprintf whereas the second one uses _printf. Hence it seems that error is due to this issue.