C isspace()

isspace() 函数在 ctype.h 头文件中定义。它用于检查指定的字符是否为空格字符。


int isspace(int argument); #where argument will be a character
 

isspace() 参数

isspace() 函数接受一个参数,其形式为整数,返回值也应为整数。当传入一个字符时,它会转换为其 ASCII 值对应的整数值。空格字符包括:

  •  ' '  -  空格
  • '\n' -  换行符
  • '\t'  -  水平制表符
  • '\v' -   垂直制表符
  • '\f'  - 换页符
  • '\r'  -  回车符
参数 描述 必需/可选
参数 要检查的字符 必需

isspace() 返回值

 如果给定字符是空格,isspace() 返回一个非零整数,否则返回零。当传入一个空格字符时,我们将得到一个不同的非零整数。

输入 返回值
如果参数不是空格
非零数字 如果参数是空格

isspace() 示例

示例 1:C 语言中 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.

示例 2:C 语言中 isspace() 函数如何工作?


#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