1. ## long double problem...

Hello all, I have just begun learning C and have run into a problem with floating-point numbers. I am using Dev-C++ 4.9.9.2 on Vista. I am working on this example from a book:

Code:
```/* showf_pt.c -- displays float value in two ways */

#include <stdio.h>

int main(void)

{

float aboat = 32000.0;

double abet = 2.14e9;

long double dip = 5.32e-5;

printf("%f can be written %e\n", aboat, aboat);

printf("%f can be written %e\n", abet, abet);

printf("%f can be written %e\n", dip, dip);

return 0;

}```
The output the book says I should get is this:

Code:
```32000.000000 can be written 3.200000e+04

2140000000.000000 can be written 2.140000e+09

0.000053 can be written 5.320000e-05```
But the output that I get is:

Code:
```32000.000000 can be written 3.200000e+004

2140000000.000000 can be written 2.140000e+009

-1950228512509697500000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000.000000
can be written 2.725000e+002```
The first two printf functions come out right, but the third one is obviously wrong. Can anyone help me figure out what is going on here? Thanks!

2. %f and %e is not meant to print long doubles.
And use code tags next time!

3. Elysia, thanks for the quick reply, and sorry about those code tags, i went back and put them in. What should i use instead of %f and %e when trying to print long doubles?

4. Originally Posted by brianedward
Elysia, thanks for the quick reply, and sorry about those code tags, i went back and put them in. What should i use instead of %f and %e when trying to print long doubles?
%lf is used for long double.

5. I tried using %lf, but the output was the same as %f. Is there something else that I'm missing?

6. It's %Lf, not %lf. In the latest revision of the C standard, %lf simply means the same thing as %f. In earlier revisions, %lf is invalid.

7. I tried the %Lf also, but with the same results. Maybe this will better illustrate the problem i'm having:
Code:
```#include <stdio.h>

int main(void)
{
long double dip = 5.32e-5;

printf("%Lf can be written as %e\n", dip, dip);

getchar();

return 0;
}```
the result is this
Code:
```-1950228512509697500000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000.000000
can be written as 2.725000e+002```
but it should be, or at least what the book says it should be:
Code:
`0.000053 can be written 5.320000e-05`
any ideas?

8. printf() is not smart. It has no idea what types you're passing to it. You have to tell it, which is what you're doing with %f, %Lf, %e, and so on. %f means you're passing a double, and if you don't pass a double, all bets are off. Similarly for %e. You must instead use %Le, which tells printf() to expect a long double, which is what you're passing.

Your compiler is not required to tell you (nor can it always tell you, even for those that do warn on improper printf() usage, like gcc) when you're calling printf() incorrectly. It's up to you to get it right, although you might try turning up your compiler's warning level; perhaps it can diagnose some incorrect printf() calls.

9. Thanks for the help cas, I changed the printf function to say:
Code:
`printf("%Lf can be written as %Le\n", dip, dip);`
But I got the exact same result as before. I must have something else wrong. Any more ideas?

10. Broken compiler? If the following code doesn't print what you'd expect, there's a bug somewhere (in the compiler or library, probably):
Code:
```#include <stdio.h>
int main(void)
{
long double d = 5.32e-5;
printf("%Lf\n", d);
return 0;
}```
Edit: It might also be a mismatch between compiler and library; that is, if your compiler relies on a system library, or a library from another compiler, they might disagree about what, exactly, a long double is. If they are at odds on this issue, then you wouldn't expect correct output.

11. You must be right, because I got the same result as before. I'm using Dev-C++ 4.9.9.2, should I just try to re-install it, or should i try a different compiler?

12. This is what happened the last time somebody asked this question.

13. Originally Posted by tabstop
This is what happened the last time somebody asked this question.
Oh wow, manual deconstruction. That's a nasty thing to have to do.

14. Originally Posted by cas
It's %Lf, not %lf. In the latest revision of the C standard, %lf simply means the same thing as %f. In earlier revisions, %lf is invalid.
In my case, both %Lf and %lf print the same thing without any warning with %lf. I compiled the OP's code
Code:
```#include <stdio.h>

int main(void)
{
long double dip = 5.32e-5;

printf("%Lf can be written as %e\n", dip, dip);//with %lf also the same result

getchar();

return 0;
}```
The answer is correct with both of them.
Code:
`0.000053 can be written 5.320000e-05`

15. Originally Posted by BEN10
In my case, both %Lf and %lf print the same thing without any warning with %lf.
That's one possibility when the behavior is undefined.

A compiler is not required to diagnose mismatching/incorrect conversion specifiers in calls to printf(). %lf was a common extension in C89 which meant the same as %f, although it's certainly possible that an implementation would have %lf mean the same as %Lf (%lf is required to mean %f in C99). Your system might also treat double and long double as the same thing, which would mean %Lf and %f would work for either.

For reference, the OP's code gives me:
0.000053 can be written as -1.950229e+150 (that's with %Lf and %e)
or
-19502285125096974860202976549594398724180239944301 48306244153100897726713609013030397828640261329800 79742015910180161347640232760093790116131317271756 8.000000 can be written as 2.725000e+02 (%lf and %e)