5

In C you have the "%c" and "%f" formats flags for printf- and scanf-like functions. Both of these function use variable length arguments ..., which always convert floats to doubles and chars to ints.

My question is, if this conversion occurs, why do separate flags for char and float exist? Why not just use the same flags as for int and double?

Related question:
Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?

Community
  • 1
  • 1
Paul Manta
  • 30,618
  • 31
  • 128
  • 208

2 Answers2

10

Because the way it gets printed out is different.

printf("%d \n",100); //prints 100
printf("%c \n",100); //prints d - the ascii character represented by 100
Sam Mussmann
  • 5,883
  • 2
  • 29
  • 43
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

Because float and double have different machine representations or sizes, and calling conventions: many processors have registers dedicated to floating point which might be used for argument passing.

And the C standard requires that short arguments are converted to int and float arguments are converted to double.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 3
    Note that these conversions aren't done for all arguments, only for arguments where the function declaration doesn't specify the parameter type. – Keith Thompson Jan 21 '12 at 09:38
  • @Keith: what's relevant to this question is that the promotions are always applied to varargs. Which as you say is a subset of "arguments where the function declaration doesn't specify the parameter type". – Steve Jessop Jan 21 '12 at 12:55