0

I've read that printf's %f specifier is used for double and that it automatically converts float to double. But I couldn't find any proof in books (though it's stated on the web).

I don't have access to a copy of the ANSI C standard files and I couldn't find the specific place in the glibc source code. I have searched in "The C Programming Language, 2nd Edition".

I need to have something for ANSI C specifically.

As effeffe pointed out, there is a paragraph in the C89 standard, 3.3.2.2 which states what I was looking for. In C99 it's just a different paragraph.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • 1
    You're looking at the wrong thing - it's not printf that does the conversion. It's argument promotion that happens for functions with `...` arguments. The answer in the linked question has a link to a draft C standard you can check. – Mat Apr 05 '14 at 18:43
  • @Mat: How does that answer his question specifically? Are you saying that his premise is wrong, and that `%f` is merely a formatting specifier, which doesn't change the underlying data type? If that is true, then why are you pointing him to something that has no relevance? – Robert Harvey Apr 05 '14 at 18:44
  • @RobertHarvey: yes, `%f` is purely formatting. printf receives its arguments already promoted (float to double, small ints to int). The linked question affirms that, and that it is standard-mandated behavior, with a link to said standard. – Mat Apr 05 '14 at 18:47
  • To be clear, does `printf` promote the float argument to a double *because* the `%f` is specified, or does it merely display it in the float form? – Robert Harvey Apr 05 '14 at 18:48
  • @Mat that's a C99 draft. I have found this: http://port70.net/~nsz/c/c89/c89-draft.html#3.2.2.1 but it's different from the C99. –  Apr 05 '14 at 18:49
  • @ctwx: Probably because that's C89. – Robert Harvey Apr 05 '14 at 18:51
  • 2
    @RobertHarvey: `printf` doesn't receive a float at all, it receives a double. – Mat Apr 05 '14 at 18:51
  • @RobertHarvey that's why I asked. I can't take the C99 draft as a source when talking about ANSI C. So I am searching for papers, books or something that states something similar. –  Apr 05 '14 at 19:12
  • Sounds like you need to purchase the actual specification. – Robert Harvey Apr 05 '14 at 19:13
  • @RobertHarvey OK thanks. That's at least a start. I hoped that there'd be something anybody can access. Thanks! –  Apr 05 '14 at 19:14
  • 1
    The linked answer refers to C99 6.5.2.2 "Function calls": C89 describes the same behavior in a section with the same title, it just has a different number, 3.3.2.2... – effeffe Apr 05 '14 at 19:42
  • 1
    @Ctwx: if you need an inexpensive official copy of a C or C++ standard document, I try to track those in this SO post: http://stackoverflow.com/a/83763/12711 However, keep in mind that [the standard committee says this](http://www.open-std.org/jtc1/sc22/wg14/www/standards.html) about [WG14 N1256](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf): "The latest publicly available version of the C99 standard is the combined C99 + TC1 + TC2 + TC3, WG14 N1256, dated 2007-09-07. This is a WG14 working paper, **but it reflects the consolidated standard at the time of issue**" – Michael Burr Apr 05 '14 at 19:56

2 Answers2

1

It's not the printf function .Default argument promotion in functions with variable parameters uses a temporary double variable ,initialized with the float that you pass in , causing the floats to be treated like doubles.

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.

References: ISO Sec. 6.3.2.2 H&S Sec. 6.3.5 p. 177, Sec. 9.4 pp. 272-3

C Standard. Chapter 6.5.2.2.

ColonelMo
  • 134
  • 1
  • 9
0

Take a look at the description of printf and the format specifiers.

It lists f to be used for decimal floating point. It doesn't say whether it is appropriate for float or double.

A little ways down, there is a table in that page that describes the length sub-specifier, such as %lf. It shows:

lengh sub-specifiers

Adding the l sub-specifier does not change the meaning of %f. It also indicates that %f is used for doubles not floats. Conversion of a float to a double is implied from this.

R Sahu
  • 204,454
  • 14
  • 159
  • 270