isspace in C Programming

The C isspace function is one of the Standard Library Functions available in C programming, which is useful to check whether the given character is a space or not. The basic syntax of the C isspace is as shown below.

The below isspace function in C Programming language will accept a single character as the parameter and find whether the given character is a space or not.

isspace(char)

isspace in C Programming Example

The C isspace function is used to find whether the given character is a space character or not.

#include <stdio.h>
#include <ctype.h>

int main()
{
    char ch = ' ';
    
    if(isspace(ch))
    {
        printf("The Character ch is a Space Character \n");
    }
    else
    {
        printf("The Character ch is Not a Space Character \n");
    }
    return 0;
}
The Character ch is a Space Character 

isspace Example 2

This C program allows the user to enter any character and check whether the character is space or not using the isspace function.

#include <stdio.h>
#include <ctype.h>

int main()
{
    char ch;
    printf("Please Enter Any Valid Character : \n");
    scanf("%c", &ch);

    if(isspace(ch))
    {
      printf("\n You have entered a Space Character");         
    }
    else
    {
      printf("\n %c is not a Space Character", ch);
      printf("\n I request you to enter Space Character");	
    }
}
isspace in C programming 2

Let me enter the Uppercase letter.

Please Enter Any Valid Character : 
T

 T is not a Space Character
 I request you to enter Space Character

First, we declared a character variable called ch.

char ch;

The first C Programming statement will ask the user to enter any character. And then, we are using the scanf to assign the user entered character to the ch variable.

printf("Please Enter Any Valid Character : \n");
scanf("%c", &ch);

Next, we used the If Statement to check whether the character is space or not using the isspace function. If the condition is True, then the following statement will be printed.

printf("\n You have entered a Space Character");

If the above condition is FALSE, then the given character is not space. So, it will print the below statements.

printf("\n %c is not a Space Character", ch);
printf("\n I request you to enter Space Character");

Without using isspace in C Example

This C program allows the user to enter any character and check whether the character is space or not using the ASCII table. Here we don’t use the built-in function isspace.

#include <stdio.h>
#include <ctype.h>

int main()
{
    char ch;
    printf("Please Enter Any Valid Character : \n");
    scanf("%c", &ch);

    if(ch == 32)
    {
      printf("\n You have entered a Space Character");         
    }
    else
    {
      printf("\n %c is not a Space Character", ch);
      printf("\n I request you to enter Space Character");	
    }
}
Please Enter Any Valid Character : 
 

 You have entered a Space Character

Since 32 is the ASCII value for space, we replaced the isspace function with 32. I suggest you cross-check the ASCII table. Let me try another value.

Please Enter Any Valid Character : 
m

 m is not a Space Character
 I request you to enter Space Character