为什么putchar(),fputc()和putc()的参数类型不是char? [英] Why argument type of `putchar()`, `fputc()` and `putc()` is not `char`?

查看:130
本文介绍了为什么putchar(),fputc()和putc()的参数类型不是char?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Does anybody know Why argument type of putchar(), fputc() and putc() is not char, but argument type of putwchar(), fputwc() and putwc() is wchar_t? See also this and this.

推荐答案

答案是旧版"(或历史").在C90标准之前,没有函数原型,并且所有函数的所有参数均受默认提升规则的约束,因此char会自动作为int传递(short也会被提升为int,而floatdouble,以及类似的无符号类型).该标准无法破坏现有代码,因此它为这些功能保留了该类型.在实践中几乎没有什么区别.即使您传递的值超出范围,您传递的值也将被视为字符类型. fputc(int c, FILE *stream)的规范说:

The answer is 'legacy' (or 'history'). Before the C90 standard, there were no function prototypes and all arguments to all functions were subject to default promotion rules, so a char was automatically passed as an int (short was promoted to int too, and float to double, and similarly for unsigned types). The standard couldn't afford to break existing code, so it kept that type for these functions. It makes very little difference in practice. The value you pass will be treated as a character type even if you pass a value that's out of range. The specification of fputc(int c, FILE *stream) says:

fputc函数将c指定的字符(转换为unsigned char)写入到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函数调用

¶6如果表示被调用函数的表达式的类型不包含 原型,将对每个参数执行整数提升,并且 具有float的类型被提升为double.这些称为默认参数 促销活动.

§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…函数原型声明符中的省略号引起 参数类型转换在最后声明的参数之后停止.默认参数 促销是在尾随参数上进行的.

¶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中定义

Integer promotions are defined in §6.3.1

¶2在可以使用intunsigned int的表达式中,可以使用以下内容:

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

  • 具有整数类型(intunsigned int除外)的对象或表达式,其整数转换等级小于或等于intunsigned int的等级.
  • _Boolintsigned intunsigned int类型的位字段.
  • 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.

如果int可以表示原始类型的所有值(受宽度限制,对于 位字段),将值转换为int;否则,它将转换为unsigned int.这些称为整数促销. 58) 整数促销.

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整数促销保留包括符号在内的值.如前所述,是否 纯" char被视为由实现定义的已签名.

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

58)整数提升仅适用于:作为常规算术转换的一部分,某些 参数表达式,一元+-~运算符的操作数,以及 移位运算符,如其各自的子句所指定.

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个要点.

The integer ranks are defined in ¶1 of the section in 10 bullet points.

这篇关于为什么putchar(),fputc()和putc()的参数类型不是char?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆