C isspace()

The isspace() function defined in the ctype.h header file. It helps to check the specified character is a white-space character or not.


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

isspace() Parameters:

The isspace() function takes a single parameter and is in the form of an integer and the return value should be an integer. When a character is passed it is converted into the integer value corresponding to its ASCII value. The white-space characters are,

  •  ' '  -  space
  • '\n' -  newline
  • '\t'  -  horizontal tab
  • '\v' -   vertical tab
  • '\f'  - form feed
  • '\r'  -  Carraige return
Parameter Description Required / Optional
argument  The character to be checked Required

isspace() Return Value

 If the given character is a white-space, isspace() returns a non-zero integer else zero. When a white-space character is passed we will get a different non-zero integer.

Input Return Value
Zero If the parameter isn't a white-space
Non zero number If the parameter is a white-space

Examples of isspace()

Example 1: Working of isspace() function in C


#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;
}


Output:


Please enter a character: 7
The given character is not a white-space.

Example 2: How isspace() function works in C?


#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;
}

Output:


\n is a white-space