为什么的putchar,TOUPPER,tolower的,等取为int的,而不是一个char? [英] Why putchar, toupper, tolower, etc. take a int instead of a char?

查看:441
本文介绍了为什么的putchar,TOUPPER,tolower的,等取为int的,而不是一个char?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,字符串是字符数组(的char * )和人物通常都存储在字符。我注意到,从libc中的一些功能正在作为参数,而不是整数一个char的。

例如,让我们的功能 TOUPPER() tolower的(),无论使用 INT 。该名男子网页显示:


  

如果c不是unsigned char值,或EOF,这些行为
  功能是不确定的。


我的猜测是,以 INT TOUPPER tolower的能够处理 unsigned char型 EOF 。但事实上 EOF 在实践中(有没有关于它的价值的任何规则?)可以存储与字符,而且由于这些功能不会改变 EOF 成别的东西,我不知道为什么 TOUPPER 不随便拿个字符作为参数。<​​/ p>

在任何情况下,我们为什么还需要接受的东西是不是一个字符(如EOF)?可能有人向我提供相关的使用案例?

这是的fputc 的putchar ,也采取了 INT 被转换成 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 in char. 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() and tolower() that both use int. 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 and tolower are able to deal with unsigned char and EOF. But in fact EOF is in practice (is there any rule about its value?) a value that can be stored with a char, and since those functions won't transform EOF into something else, I'm wondering why toupper 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 or putchar, that also take a int that is converted into an unsigned 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屋!

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