如果“char”转换为“int”,为什么“printf”中存在“%c”?

发布于 2024-12-27 23:19:38 字数 543 浏览 4 评论 0原文

在 C 中,您有 "%c""%f" 格式标志用于 printf- 和 scanf-像函数一样。这两个函数都使用可变长度参数 ...,它总是将 floats 转换为 doubles 并将 chars 转换为 <代码>整数。

我的问题是,如果发生这种转换,为什么 charfloat 存在单独的标志?为什么不直接使用与 intdouble 相同的标志呢?

相关问题:
为什么 scanf() 需要“%lf”来表示双打,而 printf() 只需使用“%f”就可以了?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风柔一江水 2025-01-03 23:19:38

因为打印出来的方式不一样。

printf("%d \n",100); //prints 100
printf("%c \n",100); //prints d - the ascii character represented by 100

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
神回复 2025-01-03 23:19:38

因为 floatdouble 具有不同的机器表示或大小以及调用约定:许多处理器都有专用于浮点的寄存器,可用于参数传递。

C 标准要求将 short 参数转换为 int,并将 float 参数转换为 double

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文