5

[Update] The results would still be the same no matter I use lf, Lf, llf...I am using Code Blocks to compile and run my C program. I am using GNU GCC compiler.

I tried to printf long double float type on different computers including two Windows and one Mac but it turns out that none of them is working as I expected. The code is the following:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    double d = 6.232345235;   //8 bytes
    long double ld = 5.5767458458;

    printf("%lf\n", ld);
    return 0;
}

Either the return value is -0.000000 or another very huge negative number which I don't remember now.

Any help is greatly appreciated! :D

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
elfsummer
  • 147
  • 1
  • 4
  • 10
  • asked before: http://stackoverflow.com/questions/4089174/printf-and-long-double – Kyle Coventry Jan 23 '15 at 23:50
  • What compiler are you using, and how are you invoking it? – Iharob Al Asimi Jan 23 '15 at 23:56
  • Yeah I've seen that post before but it just didn't solve my problem here... – elfsummer Jan 23 '15 at 23:57
  • I am using GNU GCC compiler – elfsummer Jan 23 '15 at 23:57
  • Are you invoking the compiler directly from the command line? – Iharob Al Asimi Jan 23 '15 at 23:59
  • I did not using command line. Instead, I use the code blocks as the IDE – elfsummer Jan 24 '15 at 00:01
  • It might be an IDE problem then, try compiling and running from command line, or with a different IDE. – Iharob Al Asimi Jan 24 '15 at 00:02
  • Actually I am learning this example from the following tutorial on youtube https://www.youtube.com/watch?v=2A8fi0X23Jc&index=40&list=PL0170B6E7DD6D8810 during which the presenter shows how he could use lf but I cannot repeat such example – elfsummer Jan 24 '15 at 00:03
  • Most of the things said in that tutorial are wrong, `long double` is not `12` bytes wide, and it's not true that you can represent only 6 decimal digits in a `float` think about it, why is it called floating point then? Check [this](http://en.wikipedia.org/wiki/IEEE_floating_point). If you really want to learn read a book instead of listening to this kind of tutorials. – Iharob Al Asimi Jan 24 '15 at 00:05
  • Thanks! Can you recommend some book for C beginner? I know fortran... – elfsummer Jan 24 '15 at 00:18
  • The format specifier `%llf` used in the video is not a standard notation for printing `long double`. It presumably works on the machine used in the tutorial (though frankly I couldn't read the evidentiary black screens), but not in general. – Jonathan Leffler Jan 24 '15 at 00:27
  • For books, see [The Definitive C Book and Guide List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – Jonathan Leffler Jan 24 '15 at 00:28

1 Answers1

11

The right format specifier for long double in printf() is "%Lf"

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • no, it doesn't work this way either...and it gives the same results no matter I use lf or Lf...I was expecting other explanations or tests I could do – elfsummer Jan 23 '15 at 23:51
  • 3
    @elfsummer: then you need to identify the compiler, C library and platform you are using. The C standard says that `"%Lf"` is correct for `long double`. If that does not work on your system, then either (a) your system is broken or (b) you made a mistake such as not recompiling after editing. With all due respect, (b) is more likely than (a), though the other is not actually impossible. But without evidence to the contrary (actual code, sample output), it will be hard to believe that your implementation is that broken. – Jonathan Leffler Jan 23 '15 at 23:54
  • a) I don't think my system is broken since I've tried it using code blocks on three different but properly-working computers. b) I clicked the button "compile and run" each time – elfsummer Jan 24 '15 at 00:00
  • 1
    @elfsummer: I compiled the following code on Mac OS X 10.9.4 with GCC 4.9.1: `#include int main(void) { long double ld = 5.5767458458; printf("%.9Lf\n", ld); return 0; }` and when I ran it, the output was `5.576745846`, which is correct (and if I'd printed 10 digits instead of 9, I'm sure that would be correct too). I also compiled with `clang` from XCode 6.1.1 and with `"%.13Lf"` got the number specified plus 3 trailing zeros. – Jonathan Leffler Jan 24 '15 at 00:00
  • @elfsummer: Note that `"%lf"` is effectively a synonym for `"%f"` and is only correct for `double`, not for `long double`. With Mac OS X, `long double` is a 16-byte quantity, not a 12-byte quantity as the video states. Could you update the code with more precise version information for GCC (`gcc --version` tells you). – Jonathan Leffler Jan 24 '15 at 00:06
  • Yeah I tried this example on a command prompt directly instead of using IDE then the results are correct as you suggested. GCC version is i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.9.00) – elfsummer Jan 24 '15 at 00:19
  • Thanks for your suggestions! Can you recommend some books for C beginners? – elfsummer Jan 24 '15 at 00:19
  • @elfsummer , Hover over the [c] tag and click "tag wiki". Scroll down to see good books. – Spikatrix Jan 24 '15 at 03:02
  • Compile it with -Wall option – CocoCrisp Mar 01 '19 at 18:33