45

From a previous question:

If you attempt to pass a float to printf, it'll be promoted to double before printf receives it

printf() is a variadic function right? So does a variadic function promote a float argument to a double before passing it?

18
  • 7
    Yes, via the "default argument promotions". Commented Jan 22, 2015 at 20:00
  • 2
    Similarly, char is promoted to int, so char c=127; printf("%d", c); works correctly too. Commented Jan 22, 2015 at 20:06
  • 1
    would you rather demote a double to a float? Would you rather add the complexity of having each type independent of other, closely related types? Promoting floats seems like the common-sense solution to me. Commented Jan 22, 2015 at 21:16
  • 2
    I don't think the quote is not entirely correct (or doesn't include complete context); float values are not always converted to double when passed to a function, but they are when passed as trailing arguments to a var-args function like printf. Commented Dec 22, 2016 at 14:42
  • 5
    Your quote is misquoted, or perhaps from an old edition. Googling C Primer Plus turns up a PDF of the fifth edition where the actual quote is "That's because C automatically expands type float values to type double when they are passed as arguments to any function, such as printf(), that doesn't explicitly prototype the argument type." Commented Dec 22, 2016 at 15:21

4 Answers 4

39

Yes, float arguments to variadic function are promoted to double.

The draft C99 standard section 6.5.2.2 Function calls says:

[...]and arguments that have type float are promoted to double. These are called the default argument promotions.[...]

from the draft C++ standard section 5.2.2 Function call:

[...]a floating point type that is subject to the floating point promotion (4.6), the value of the argument is converted to the promoted type before the call. [...]

and section 4.6:

A prvalue of type float can be converted to a prvalue of type double. The value is unchanged

cppreference covers the default conversions for variadic function in C++ well:

  • std::nullptr_t is converted to void*
  • float arguments are converted to double as in floating-point promotion
  • bool, char, short, and unscoped enumerations are converted to int or wider integer types as in integer promotion

We can see in C and presumably in C++ this conversion was kept around for compatibility with K&R C, from Rationale for International Standard—Programming Languages—C (emphasis mine):

For compatibility with past practice, all argument promotions occur as described in K&R in the absence of a prototype declaration, including the not always desirable promotion of float to double.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, float arguments to variadic function are promoted to double. Only in trailing arguments. Please fix this.
Man I learn something substantially new about C/C++ everytime. Was wondering why x86 assembly code wasn't printing floats properly. I did alot of testing and it came to mind; I wonder what would happen if I tried to print 64bit floats. It worked. Then I questioned how come there is no specific format specifier for float32 vs float64; and came here to learn that floats are really promoted to float64 if they are variadic arguments.
24

As for the why part of the question, it's simple: the C (and C++) standards consider double to be the "default" floating point type. Not float (which is what many of us programmers default to when using floating point numbers).

This can be seen by observing:

  1. 3.14 is a double (if you want a float, you've got to take an extra step and append an f)
  2. The standard math functions take a double by default (for example, sin() takes a double; if you want a float you've got to use sinf())

With this, it seems more "natural" that a float would be promoted to double in a variadic function call, given that double is the "natural" default in the language.

2 Comments

"Not float is what many of us programmers default to when using floating point numbers)." I don't know how others do it, but I ususally take double. I only resort to float if it is about storing e. g. lots of measurement data with a quite high intrinsic error which need to be stored in an file.
One of my big regrets about C is that when long double was added, no mechanism was included to specify whether a variadic function expected all floating-point values to be converted to the longest type (long double), specific type double, or that it expected long double and double to be passed differently. IMHO, a lot of time and portability headaches would have been saved over the years if code could have said "convert everything integer-ish to this type and everything float-ish to this type".
17

Given a function prototype, type float is only automatically promoted1 when used in trailing arguments. Function print uses those:

int printf(const char * restrict format, ...); 

1 (Quoted from: ISO/IEC 9899:201x 6.5.2.2 Function calls)
6. 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.
7. The default argument promotions are performed on trailing arguments.

Comments

12

Because the (C99 or C11) standard says so. See answer by 2501.

There are several pragmatical reasons for that: history (first implementations of C have been used for system programming, where floating point operations don't matter), and the fact that on current (tablet, desktop, server...) processors, arithmetic operations on double are about as efficient as float (but some cheap microcontrollers don't have any FPU, or can only add float by hardware, and require a library for every operation on double). At last, I guess that such a rule enables slightly simpler calling conventions and ABIs.

Think of float as a sort-of short double (which of course is illegal in C). A float is useful mostly when you need to compact memory (and can afford the loss of precision). See also http://floating-point-gui.de/ for more.

8 Comments

Where C99 or C11 standard says so ? Will you please include an exact citation from standard ?
"Because the [...] standard says so". So does "The C Programming Language" (1st edition, 1978) by Kernighan and Ritchie.
Note: also "char" and "short" are automatically expanded and passed as "int".
The C standard does not say so. The quote is either misquoted or taken from an edition where that line was wrong.
I believe the promotion happens only for the variadic args in variadic functions like printf. If you have a function prototyped to take a float, a float will be passed.
|