Int到char转换,同时仍在c语言中使用%c

Jan*_*Doe 0 c char

我有以下代码.

int ant = 10;
char converter = ant;
printf("This is where the char prints: %c", converter);
Run Code Online (Sandbox Code Playgroud)

为什么控制台仅在运行时打印出来:

This is where the char prints:
Run Code Online (Sandbox Code Playgroud)

为什么会这样?是否仍然可以使用%c并打印出值?我知道将%c更改为%d将允许我查看结果,但我想知道原因.

Bat*_*eba 6

您正在打印由数字10 编码的字符:格式说明符%c用于输出字符,而不是它们的数值.

在ASCII中(很可能是使用的编码),那是\n; 换行符.你的终端可能能够处理它,你会在输出中看到一个额外的行.

如果要打印数值converter,则仅使用%d格式说明符(char类型被隐式转换int为调用站点的类型).

  • @JaneDoe - `%c`用于打印*单个字符*.如果你期望它(单个'%c`)输出`10'(两个字符),就没有办法实现它. (5认同)