gpt4 book ai didi

c - 为什么 `putchar()` 、 `fputc()` 和 `putc()` 的参数类型不是 `char` ?

转载 作者:太空狗 更新时间:2023-10-29 16:05:52 25 4
gpt4 key购买 nike

有谁知道为什么 putchar()fputc()putc() 的参数类型不是 char,但是putwchar()fputwc()putwc()的参数类型是wchar_t?另见 thisthis .

最佳答案

答案是“遗产”(或“历史”)。在 C90 标准之前,没有函数原型(prototype),所有函数的所有参数都遵循默认提升规则,因此 char 自 Action 为 int 传递( short 也被提升为 intfloat 被提升为 double,无符号类型也类似)。该标准不能破坏现有代码,因此它为这些功能保留了该类型。它在实践中几乎没有什么区别。即使您传递的值超出范围,您传递的值也将被视为字符类型。 fputc(int c, FILE *stream) 的规范说:

The fputc function writes the character specified by c (converted to an unsigned char) to the output stream pointed to by stream

默认促销规则

§6.5.2.2 Function calls

¶6 If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions.

¶7 … The ellipsis notation in a function prototype declarator causes argument type conversion to stop after the last declared parameter. The default argument promotions are performed on trailing arguments.

整数提升在 §6.3.1 中定义

¶2 The following may be used in an expression wherever an int or unsigned int may be used:

  • An object or expression with an integer type (other than int or unsigned int) whose integer conversion rank is less than or equal to the rank of int and unsigned int.
  • A bit-field of type _Bool, int, signed int, or unsigned int.

If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.58) All other types are unchanged by the integer promotions.

¶3 The integer promotions preserve value including sign. As discussed earlier, whether a 'plain' char is treated as signed is implementation-defined.

58) The integer promotions are applied only: as part of the usual arithmetic conversions, to certain argument expressions, to the operands of the unary +, -, and ~ operators, and to both operands of the shift operators, as specified by their respective subclauses.

整数排名在本节的 ¶1 中的 10 个要点中定义。

关于c - 为什么 `putchar()` 、 `fputc()` 和 `putc()` 的参数类型不是 `char` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40712493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com