malat
malat

Reputation: 12510

`l` length modifier has no effect on a following a, A, e, E, f, F, g, or G conversion specifier

I am trying to understand the following section:

l (ell)

Specifies that a following d, i, o, u, x, or X conversion specifier applies to a long or unsigned long argument; that a following n conversion specifier applies to a pointer to a long 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.

I can also see that this is consistent with cppreference:fprintf where "%f" and "%lf" are equivalent for printf() family.

So is this answer erroneous ? Or does C99 makes it clear now that "%f" is for float, while "%lf" is for double for printf() family functions ?

Upvotes: 2

Views: 155

Answers (2)

Sourav Ghosh
Sourav Ghosh

Reputation: 134396

TL;DR - The answer that you linked is perfectly fine. %f in printf() can handle double and float, both type of argument.


Information: printf() is a variadic function.

As per C99, chapter 7.19.6.3

Syntax

#include <stdio.h>
int printf(const char * restrict format, ...);

and regarding the %f format specifier,

f,F

A double argument representing a floating-point number is converted to decimal notation in the style [−]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. [....]

So, we see, the standard only mentions double. Curious about its younger counterpart, float?

Then, from chapter §6.5.2.2, paragraph 7

[...] The ellipsis notation in a function prototype declarator causes argument type conversion to stop after the last declared parameter. The default argument promotions are performed on trailing arguments.

and, regarding the default argument promotions part, (emphasis mine), paragraph 6,

[...] If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions. [...]

so, for printf() function, with %f format specifier, it does not matter whether the supplied argument is of type float or double, it will get promoted to double anyway.

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727047

The documentation is correct: printf sees no difference between float and double.

Parameters of functions from the printf family are taken through the variable-length mechanism. In the variable-length part of a variable-length argument list, the "default argument promotions" apply: types char and short int are promoted to int, and float is promoted to double.

Upvotes: 6

Related Questions