iscntrl 的行为是什么?

now*_*wox 0 c character standard-library

功能iscntrl标准化。不幸的是,在 C99 上我们有:

iscntrl 函数测试任何控制字符

考虑到原型,int iscntrl(int c);我期待true0..31 或 127 之类的东西。然而在以下情况中:

#include <stdio.h>
#include <ctype.h>
int main()
{
    int i;
    printf("The ASCII value of all control characters are ");
    for (i=0; i<=1024; ++i)
    {
        if (iscntrl(i)!=0)
            printf("%d ", i);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到这个输出:

The ASCII value of all control characters are 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 127 264 288 308 310 320 334 
336 346 348 372 374 390 398 404 406 412 420 428 436 444 452 458 460 466 468 474 
476 484 492 500 506 512 518 530 536 542 638 644 656 662 668 682 688 694 700 706 
708 714 716 718 760 774 780 782 788 798 826 834 836 846 854 856 864 866 874 876 
882 888 890 892 898 900 908 962 968 970 988 994 1000
Run Code Online (Sandbox Code Playgroud)

所以我想知道这个功能在幕后是如何实现的。我尝试在标准库上搜索,但答案并不明显。

https://github.com/bminor/glibc/search?q=iscntrl&unscoped_q=iscntrl

有任何想法吗?

And*_*nle 6

您通过向 传递不正确的值来调用未定义的行为iscntrl()

根据7.4 字符处理<ctype.h>第 1 段

标头<ctype.h>声明了几个对于分类和映射字符有用的函数。在所有情况下,参数都是 an int,其值应可表示为 unsigned char 或应等于宏的值EOF。如果参数具有任何其他值,则行为未定义。