Home > Software engineering >  difference between %fl and %lf in C
difference between %fl and %lf in C

Time:10-25

I am currently learning about C language data type and I try to print a double variable the compiler suggested me to use fl after I type '%', and I got 1 number in the end of precision decimal line. Compare to %lf it will print six precision decimals in total

double num=12322;
printf("%lf",num);// result is 12322.000000
printf("%fl",num);// result is 12322.0000001 

I've searched plenty of place but mostly the difference between %f and %lf is frequently asked. Is my situation could possiply the same?

CodePudding user response:

In this call of printf

printf("%lf",num);// result is 12322.000000

the length modifier l in the conversion specifier %lf has no effect.

From the C Standard (7.21.6.1 The fprintf function)

7 The length modifiers and their meanings are:

l (ell) Specifies that a following d, i, o, u, x, or X conversion specifier applies to a long int or unsigned long int argument; that a following n conversion specifier applies to a pointer to a long int argument; that a following c conversion specifier applies to a wint_t argument; that a following s conversion specifier applies to a pointer to a wchar_t argument; or has no effect on a following a, A, e, E, f, F, g, or G conversion specifier.

In this call of printf

printf("%fl",num);// result is 12322.0000001 

where in the comment there shall be written the letter 'l' instead of the number 1 as you think

// result is 12322.000000l
                        ^^^

the format string "%fl" means that after outputting an object of the type double due to the conversion specification %f there will be outputted the letter 'l'.

Pay attention to that with the conversion specifier f there can be used one more letter 'l' that is the upper case letter 'L'. In this case the conversion specification %Lf serves to output objects of the type long double.

CodePudding user response:

I think you actually have a typo in your output....

double num=12322;
printf("%lf",num);// result is 12322.000000
printf("%fl",num);// result is 12322.0000001

is actually

double num=12322;
printf("%lf",num);// result is 12322.000000
printf("%fl",num);// result is 12322.000000l

The C standard says that the float is converted to a double when passed to a variadic function, so %lf and %f are equivalent; %fl is the same a %f... with an l after it.

CodePudding user response:

There are two correct ways of printing a value of type double:

printf("%f", num);

or

printf("%lf", num);

These two have exactly the same effect. In this case, the "l" modifier is effectively ignored.

The reason they have the same effect is that printf is special. printf accepts a variable number of arguments, and for such functions, C always applies the default argument promotions. This means that all integer types smaller than int are promoted to int, and float is promoted to double. So if you write

float f = 1.5;
printf("%f\n", f);

then f is promoted to double before being passed. So inside printf, it always gets a double. So %f will never see a float, always a double. So %f is written to expect a double, so it ends up working for both float and double. So you don't need a l modifier to say which. But that's kind of confusing, so the Standard says you can put the l there if you want to — but you don't have to, and it doesn't do anything.

(This is all very different, by the way, from scanf, where %f and %lf are totally different, and must be explicitly matched to arguments of type float * versus double *.)

I have no idea why your IDE complained about (put a red line under) %lf, and I have no idea what it meant by suggesting, as you said,

fl, lg, l, f,
elf, Alf, ls, sf,
if, la, lo, of

Some of those look they might be nonstandard, system-specific extensions, but some (especially fl) are nonsense. So, bottom line, it sounds like your IDE's suggestion was confusing, unnecessary, and quite possibly wrong.

  • Related