Paul Manta
Paul Manta

Reputation: 31597

Why does `"%c"` exist in `printf` if `char` is converted to `int`?

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"?

Upvotes: 5

Views: 10813

Answers (2)

Luchian Grigore
Luchian Grigore

Reputation: 258648

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

Upvotes: 10

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.

Upvotes: 0

Related Questions