50

Since it isn't strongly typed I thought it just picked the right memory size and interpreted it based on the type of argument. But float and double both use %f and they are different sizes.

P.S. I can see how promotion via copying the value to a temp and casting(is this right?) might work but how does it work for scanfs/sscanf?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141
  • [Why does printf() promote a float to a double?](http://stackoverflow.com/q/28097564/995714) – phuclv Mar 25 '17 at 02:51

1 Answers1

72

It doesn't differentiate. It's not possible to receive a float as a vararg: any float argument that you provide is first promoted to double.

6.5.2.2/6 defines "default argument promotions", and /7 states that default argument promotions are applied to "trailing arguments", that is varargs denoted by ....

how does it work for scanfs/sscanf?

The %f format for scanf requires a pointer to float. %lf requires a pointer to double, %Lf requires a pointer to long double.

copying the value to a temp and casting(is this right?)

If you provide a float argument, then the implementation creates a temporary of type double, initializes it with the float value, and passes this as the vararg. Casting by definition is explicit conversion by use of the cast operator -- you can cast if you like in order to make it exactly clear to the reader what's going on, but float f = 3; printf("%f", f); is exactly the same as float f = 3; printf("%f", (double)f);. The default argument promotion has the same meaning as the cast.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • Thanks, do printfs(standard or extensions) accept that for double as well to keep symmetry? – Roman A. Taycher Jun 18 '11 at 11:59
  • 8
    Oh, if you mean "can I use %lf and %Lf as formats in `printf` and co" then yes, you can. %lf is exactly the same as %f, both take a `double`. %Lf takes a `long double`. – Steve Jessop Jun 18 '11 at 12:39