format for double for printf

R

RoS

the format for conversion of double in the function printf is
"%f" or "%lf" or both?

for example
printf("%f", (double)0.0);
printf("%lf", (double)0.0);
are both ok?

Good morning
Thank you
 
T

Thomas X. Iverson

it seems like that in c there is no need to print a double varible
with %lf format
%f is ok
but for long interger , u should use %ld
 
I

Ian Collins

RoS said:
the format for conversion of double in the function printf is
"%f" or "%lf" or both?

for example
printf("%f", (double)0.0);
printf("%lf", (double)0.0);
are both ok?
Yes they are equivalent, the l modifier has no effect for the f specifier.
 
P

pete

Ian said:
Yes they are equivalent,
the l modifier has no effect for the f specifier.

That's the case for C99.
For C89,
a function call to printf is undefined
if "%lf" is the first argument.
 
K

Keith Thompson

The first? "%d %lf" is not undefined?

Yes, "%d %lf" is undefined for C89/C90's printf. He said "if", not
"if and only if".

(Incidentally, given printf("%d %lf", x, y), the first *argument*
isn't %d; it's the string "%d %ld".)
 
W

Walter Roberson

That's the case for C99.
For C89,
a function call to printf is undefined
if "%lf" is the first argument.

And just to confuse matters, in C89, "%Lf" *is* defined and
indicates that the corresponding argument is long double .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top