iscntrl() 函数定义在 ctype.h 头文件中。此函数用于检查指定字符是否为控制字符。根据标准 ASCII 字符集,控制字符的 ASCII 码介于 0x00 (NUL)、0x1f (US) 和 0x7f (DEL) 之间。我们也可以说,控制字符是那些不能在屏幕上打印的字符。例如,退格、Escape、换行符等。
int iscntrl(int argument); #where argument will be a character
iscntrl() 函数接受一个整数形式的参数,返回一个整数值。当传入一个字符时,它会转换为对应的 ASCII 值整数。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| 参数 | 要检查的字符 | 必需 |
如果给定字符是控制字符,iscntrl() 返回一个非零整数,否则返回零。当传入控制字符时,我们会得到一个不同的非零整数。
| 输入 | 返回值 |
|---|---|
| 零 | 如果参数不是控制字符 |
| 非零数字 | 如果参数是控制字符 |
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch;
int output;
ch = 'A';
output = iscntrl(ch);
printf("If %c is passed to iscntrl() = %d\n", ch, output);
ch = '\n';
output = iscntrl(ch);
printf("If %c is passed to iscntrl() = %d", ch, output);
return 0;
}
输出
If A is passed to iscntrl() = 0 If is passed to iscntrl() = 1
#include <stdio.h>
#include <ctype.h>
int main()
{
int x;
printf("The ASCII value of all control characters are:\n");
for (x=0; x<=127; ++x)
{
if (iscntrl(x)!=0)
printf("%d ", x);
}
return 0;
}
输出
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