NO

Author Topic: Long Double and printf() format  (Read 13503 times)

jrockel55

  • Guest
Long Double and printf() format
« on: January 03, 2015, 05:25:17 PM »
Greetings! I am a hardware designer who uses 'C' when needed. Mostly I program for embedded systems but I am trying my hand at Pelles C for use on a windows platform. I am using version 7.00.355 (Win64).

Since most of my work will be console applications, I have been playing with scanf() and printf() formats to understand the pit falls. I have one remaining problem: printf() with a 'long double'. %Le and %Lg work as expected, but %Lf does not. Per the 'fprintf' help reference, it appears it should. "L: Specifies that a following a, A, e, E, f, F, g, or G conversion specifier applies to a long double argument."
Below is my test code and output.

// Demonstrate the scanf() with floating point
#include <stdio.h>
int main(void)
{
    float f;
    double d;
    long double ld;
   
    printf("Enter three floating points: ");
    scanf("%f %lf %Lf", &f, &d, &ld);
    printf(" float %f\n double %f\n long double %Le\n\n", f, d, ld);
    printf(" float %f\n double %f\n long double %Lf\n\n", f, d, ld);
    printf(" float %f\n double %f\n long double %Lg\n\n", f, d, ld);
    return 0;
}


Enter three floating points: 123.456 5328.3495 2.544e-12
 float 123.456001
 double 5328.349500
 long double 2.544000e-12

 float 123.456001
 double 5328.349500
 long double 0.000000

 float 123.456001
 double 5328.349500
 long double 2.544e-12

Press any key to continue...


BTW, isn't it annoying that the formatting rules for scanf() are sometimes different than for printf()? Example: %lf needed for a 'double' in scanf() but %f is needed in printf(). ('l' (ell) does not modify 'f' in a printf(). Can someone share history on this point?

So bottom line question, is the help topic in error or is the printf() in error with regard to '%Lf' or what is the correct format to NOT print in scientific notation for a 'long double'?

Thank you in advance. Source code attached.



czerny

  • Guest
Re: Long Double and printf() format
« Reply #1 on: January 03, 2015, 05:44:34 PM »
Pelles C has no long double (long double == double).

It would be nice to have at least the intel 8087 extended format.

jrockel55

  • Guest
Re: Long Double and printf() format
« Reply #2 on: January 03, 2015, 06:24:39 PM »
OK, but then the problem is really in the compiler since it requires %Le and warns on %f. So if I understand correctly, the compiler is handling the scanf() and printf() formatting correctly, but memory is only set-up for a 'double'. That implies that scanf() and printf() are grabbing too much information from memory and I should see erroneous answers. Let's see what other replies there are on this topic...

I am playing around and currently running a program to find the maximum value of different data types. I see that 'int' and 'long' are both 4 bytes. ('short' holds the 2 byte place; and 'char' is 1 byte, of course.) 'long long' is defined and is 8 bytes. I also realize that by K&R's original definition, the data length for 'int' is dependent on the processor. You would think that ANSI would have nailed that down.

Anyway, thanks very much for the reply.
(Attached is my 'size-of' code.)

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Long Double and printf() format
« Reply #3 on: January 03, 2015, 08:54:53 PM »
Code: [Select]
#include <stdio.h>

int main(int argc, char* argv[]) {

  long long q=1234567890123456789;
  printf("This is a long long:\t%lld\n", q);

  long double ld=1234567890.1234567890;
  printf("This is a long double:\t%.9f\n", ld);
}

Works in Pelles C:
This is a long long:   1234567890123456789
This is a long double:   1234567890.123456746
... but as Czerny already wrote, the long double is not that long - it's an 8-byte double.
Microsoft VC shows the same behaviour. You might find a gcc or MinGW library that has a 10-byte double.
If you are in dire need for that: MasmBasic's Str$() can do it, but it's a 32-bit library.