Блог пользователя PraveenDhinwa

Автор PraveenDhinwa, 11 лет назад, По-английски
#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 :(

  • Проголосовать: нравится
  • +12
  • Проголосовать: не нравится

»
11 лет назад, # |
Rev. 2   Проголосовать: нравится +6 Проголосовать: не нравится

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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      11 лет назад, # ^ |
        Проголосовать: нравится +3 Проголосовать: не нравится

      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 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

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