如果“char”转换为“int”,为什么“printf”中存在“%c”?
在 C 中,您有 "%c"
和 "%f"
格式标志用于 printf
- 和 scanf
-像函数一样。这两个函数都使用可变长度参数 ...
,它总是将 floats
转换为 doubles
并将 chars
转换为 <代码>整数。
我的问题是,如果发生这种转换,为什么 char
和 float
存在单独的标志?为什么不直接使用与 int
和 double
相同的标志呢?
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"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为打印出来的方式不一样。
Because the way it gets printed out is different.
因为
float
和double
具有不同的机器表示或大小以及调用约定:许多处理器都有专用于浮点的寄存器,可用于参数传递。C 标准要求将
short
参数转换为int
,并将float
参数转换为double
。Because
float
anddouble
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 toint
andfloat
arguments are converted todouble
.