gpt4 book ai didi

c - ANSI C : Why character functions accept int argument instead of char argument?

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

为什么字符函数接受 int 参数而不是 char 参数?

<ctype.h>

int isalnum(int c);
int isalpha(int c);
int iscntrl(int c);
int isdigit(int c);
int isgraph(int c);
int islower(int c);
int isprint(int c);
int ispunct(int c);
int isspace(int c);
int isupper(int c);
int isxdigit(int c);
int tolower(int c);
int toupper(int c);

最佳答案

字符和整数在 C 语言中结合得相当紧密。

当您从输入流接收到一个字符时,它必须能够表示每个字符加上文件结束符号。

这意味着 char类型不够大,所以他们使用更宽的类型。

C99 基本原理文档指出:

Since these functions are often used primarily as macros, their domain is restricted to the small positive integers representable in an unsigned char, plus the value of EOF. EOF is traditionally -1, but may be any negative integer, and hence distinguishable from any valid character code. These macros may thus be efficiently implemented by using the argument as an index into a small array of attributes.

标准本身是这样说的:

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.

关于c - ANSI C : Why character functions accept int argument instead of char argument?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9307312/

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