C program to check if a character is white-space or not

Introduction :

In this post, I will show you how to check if a character is white-space or not using isspace function. A white-space character can be a space(’ ’), horizontal tab(‘\t’), newline(‘\n’), vertical tab(‘\v’), feed(‘\f’) or carriage return(‘\r’). We will use isspace() function to check that. This function is defined is ctype header file.

Syntax of isspace :

The syntax of isspace is as below :

int isspace(int char)

It takes one integer value and returns one integer based on the provided value is white-space or not. If we pass one character, it is converted to an integer. If the return value is zero, it is false i.e. it is not a white-space. If the return value is not zero, it is a white-space.

Example program :

Let me show you one example of how it works :

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

int main()
{
    char inputChar[] = {' ', '\t', '\n', '\v', '\f', '\r', 'c'};
    for (int i = 0; i < sizeof(inputChar); i++)
    {
        printf("%d\n", isspace(inputChar[i]));
    }
}

Here, we have one array of characters inputChar with different types of characters including white-space characters. It will print the below output :

1
1
1
1
1
1
0

As you can see, it returns 0 only for the character ‘c’. For other characters, it returns 1 i.e. these are white-space characters.

c check character is space or not

Similar tutorials :