简体   繁体   English

如果`char`转换为`int`,为什么``%c'`存在于`printf`中?

[英]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. 在C中,你有printf"%c""%f"格式标志 - 和scanf的函数。 Both of these function use variable length arguments ... , which always convert floats to doubles and chars to ints . 这两个函数都使用可变长度参数... ,它总是将floats转换为doubleschars转换为ints

My question is, if this conversion occurs, why do separate flags for char and float exist? 我的问题是,如果发生这种转换,为什么存在charfloat单独标志? Why not just use the same flags as for int and double ? 为什么不使用与intdouble相同的标志?

Related question: 相关问题:
Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"? 为什么scanf()需要“%lf”用于双打,当printf()只用“%f”时可以吗?

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

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

And the C standard requires that short arguments are converted to int and float arguments are converted to double . C标准要求将short参数转换为int并将float参数转换为double

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM