According to K&R C section 1.6, a char
is a type of integer. So why do we need %c
. And why can't we use %d
for everything?

- 4,160
- 8
- 43
- 67
-
1@MaziarBouali Not necessarily. – Pubby Jun 08 '12 at 11:31
-
`printf` needs to know the size of the argument in order to print it (to cast it appropriately, so to say). A `char` has size 1, an `int` has at least that, on most machines more. Also, when using `%c` you want a character printed, not a number. In the `D` language, you would always use `%s` and let the compiler worry about the types. – Matej Jun 08 '12 at 11:32
-
1@MatejNanut No, integer types smaller than `int` are promoted to `int` when being passed into a variadic function. – Pubby Jun 08 '12 at 11:35
-
@Pubby: thank you, I didn't know that. However, there is still this ambiguity when using longs, which are integers and you can't (or shouldn't) use `%d` for them. – Matej Jun 08 '12 at 11:36
6 Answers
Because %d
will print the numeric character code of the char
:
printf("%d", 'a');
prints 97
(on an ASCII system), while
printf("%c", 'a');
prints a
.

- 355,277
- 75
- 744
- 836
-
3And don't forget `scanf`: `%d` reads and converts numeric strings, where as `%c` reads and converts individual characters. – John Bode Jun 08 '12 at 15:00
While it's an integer, the %c
interprets its numeric value as a character value for display. For instance for the character a
:
If you used %d
you'd get an integer, e.g., 97, the internal representation of the character a
vs
using %c
to display the character 'a
' itself (if using ASCII)
I.e., it's a matter of internal representation vs interpretation for external purposes (such as with printf
)

- 138,105
- 33
- 200
- 191
Roughly speaking, %c
prints the ASCII representation of the character. %d
prints its decimal value.

- 28,636
- 4
- 59
- 87
If you use %c
, you'll print (or scan) a character, or char. If you use %d
, you'll print (or scan) an integer.
printf("%d", 0x70);
How will the machine would know that you want to output a character, not its equivalent ASCII value?

- 12,748
- 25
- 83
- 121
%d think char is 32bit and print it like a integer, but %c convert int value of char to ascii character then print it.

- 1,976
- 17
- 37