1

I am using Code::Blocks 12.11 on Windows 64-bit platform for C/C++ codes. I recently came across an unexpected result for this program.

#include <stdio.h>
int main()
{
    long double number;
    number = 1.1234567;
    printf("%llf\n",number);
    return 0;
}

It should print the output 1.1234567 but it gives -0.000000, I figured out that it always gives wrong values when i use long double. This code works fine on ideone http://ideone.com/Opu8cy , Please tell me if there is a way to fix this.

Nitin Alabur
  • 5,812
  • 1
  • 34
  • 52
Atul Kumar Verma
  • 369
  • 3
  • 8
  • 23

1 Answers1

0

You should use %Lf for format, not %llf.

There could also be a bug in which

number = 1.1234567L;

could help.

PRINTF(3)

   ll     (ell-ell).  A following integer conversion corresponds to a
          long long int or unsigned long long int argument, or a
          following n conversion corresponds to a pointer to a long long
          int argument.

   L      A following a, A, e, E, f, F, g, or G conversion corresponds
          to a long double argument.  (C99 allows %LF, but SUSv2 does
          not.)

Also, in Code::Blocks you should go to Settings -> Compiler and debugger … and tune up level of warnings. (Preferably to a high level.)

You are perhaps not using GCC (mingw or the like) – but if, use -Wall -Wextra -pedantic + some -std=xxx or look at answer like this one – but for your compiler.

Community
  • 1
  • 1
Runium
  • 1,065
  • 10
  • 21
  • I am using **GCC 4.7 mingw compiler**. After using %Lf and number = 1.1234567L i get the same -0.000000 result. I also added **-Wall -Wextra -pedantic-errors**, Now it shows me few warning messages `1. unknown conversion type character 'L' in format [-Wformat] and 2. too many arguments for format [-Wformat-extra-args] ` After adding std=c99 i got correct result without any warning, can anybody tell which c standard code::blocks follows by default. Thanks a lot for suggestions **Sukminder**. – Atul Kumar Verma Nov 03 '13 at 03:21
  • @AtulKumarVerma: Seems like mingw has [trouble with long double](http://stackoverflow.com/questions/1764350/conversion-specifier-of-long-double-in-c) – Runium Nov 03 '13 at 05:02