isspace() 函数在 ctype.h 头文件中定义。它用于检查指定的字符是否为空格字符。
int isspace(int argument); #where argument will be a character
isspace() 函数接受一个参数,其形式为整数,返回值也应为整数。当传入一个字符时,它会转换为其 ASCII 值对应的整数值。空格字符包括:
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| 参数 | 要检查的字符 | 必需 |
如果给定字符是空格,isspace() 返回一个非零整数,否则返回零。当传入一个空格字符时,我们将得到一个不同的非零整数。
| 输入 | 返回值 |
|---|---|
| 零 | 如果参数不是空格 |
| 非零数字 | 如果参数是空格 |
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch;
int output;
printf("Please enter a character: ");
scanf("%c", &ch;);
output = isspace(ch);
if (output == 0)
{
printf("The given character is not a white-space.");
}
else
{
printf("The given character is a white-space.");
}
return 0;
}
输出
Please enter a character: 7 The given character is not a white-space.
#include <stdio.h>
#include <ctype.h>
int main() {
char ch;
int output;
ch = '\n';
output = isspace(c);
if (output == 0) {
printf("%c is not a white-space", ch);
} else {
printf("%c is a white-space", ch);
}
return 0;
}
输出
\n is a white-space