为什么的putchar,TOUPPER,tolower的,等取为int的,而不是一个char? [英] Why putchar, toupper, tolower, etc. take a int instead of a char?
问题描述
在C,字符串是字符数组(的char *
)和人物通常都存储在字符
。我注意到,从libc中的一些功能正在作为参数,而不是整数一个char的。
例如,让我们的功能 TOUPPER()
和 tolower的()
,无论使用 INT
。该名男子网页显示:
如果c不是unsigned char值,或EOF,这些行为
功能是不确定的。
块引用>我的猜测是,以
INT
,TOUPPER
和tolower的
能够处理unsigned char型
和EOF
。但事实上EOF
在实践中(有没有关于它的价值的任何规则?)可以存储与字符$ C $价值C>,而且由于这些功能不会改变
EOF
成别的东西,我不知道为什么TOUPPER
不随便拿个字符作为参数。</ p>在任何情况下,我们为什么还需要接受的东西是不是一个字符(如EOF)?可能有人向我提供相关的使用案例?
这是
的fputc
或的putchar
,也采取了INT 同级code>被转换成
unsigned char型
反正。我要寻找的precise动机这样的选择。我想成为服气,我不想回答这个问题,我不知道,如果有人问我一天。
解决方案C11 7.4
头
&LT;&文件ctype.h GT;
声明有用的多种功能进行分类和映射
字符。在所有情况下的参数是一个int,它的值应为
重新presentable为unsigned char或应等于值了
宏EOF。如果参数为其他值,则该行为
不确定的。
块引用>C11 7.21.1
EOF
它扩展成一个整型常量前pression,与int类型和
负值,...
块引用>C标准明确指出, EOF总是带着负值 int类型。进而,默认
字符
类型的符号性是实现定义的,所以它可能是无符号的,不能存放负值:C11 6.2.5
如果基本执行字符集的成员都存储在一个char
对象,它的价值是保证非负。如果任何其他
字符存储在字符对象,所得到的值是
实现定义但应的值的范围内的
可以在该类型psented重新$ P $。
块引用>In C, strings are arrays of char (
char *
) and characters are usually stored inchar
. I noticed that some functions from the libC are taking as argument integers instead of a char.For instance, let's take the functions
toupper()
andtolower()
that both useint
. The man page says:If c is not an unsigned char value, or EOF, the behavior of these functions is undefined.
My guess is that with a
int
,toupper
andtolower
are able to deal withunsigned char
andEOF
. But in factEOF
is in practice (is there any rule about its value?) a value that can be stored with achar
, and since those functions won't transformEOF
into something else, I'm wondering whytoupper
does not simply take a char as argument.In any case why do we need to accept something that is not a character (such as EOF)? Could someone provide me a relevant use case?
This is similar with
fputc
orputchar
, that also take aint
that is converted into anunsigned char
anyway.I am looking for the precise motivations for that choice. I want to be convinced, I don't want to answer that I don't know if someone ask me one day.
解决方案C11 7.4
The header
<ctype.h>
declares several functions useful for classifying and mapping characters. In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined.C11 7.21.1
EOF
which expands to an integer constant expression, with type int and a negative value, ...
The C standard explicitly states that EOF is always an int with negative value. And furthermore, the signedness of the default
char
type is implementation-defined, so it may be unsigned and not able to store a negative value:C11 6.2.5
If a member of the basic execution character set is stored in a char object, its value is guaranteed to be nonnegative. If any other character is stored in a char object, the resulting value is implementation-defined but shall be within the range of values that can be represented in that type.
这篇关于为什么的putchar,TOUPPER,tolower的,等取为int的,而不是一个char?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!