In this tutorial, you will learn about C library function isspace() which is a character handling function used to check whether the argument supplied is a whitespace character or not.

C Library Function isspace( )

As discussed earlier, we have to include ctype.h header file for using isspace( ) standard character library function.

#include <ctype.h>

C Library Function isspace( ): Function prototype


int isspace( int ch);

This function checks whether its argument is a whitespace character or not.
It returns a true value if ch is a whitespace character and zero (0) otherwise.

Lists of whitespace character in C


Whitespace character Description
‘ ‘ space
‘\f’ form feed
‘\n’ newline
‘\r’ carriage return
‘\t’ horizontal tab
‘\v’ vertical tab

[adsense1]

Example: Demonstration of isspace( )


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

int main()
{
   printf("Demonstration of isspace():\n");
   printf("\n%s%s%s", "form feed", isspace('\f') ? " is a " : " is not a ", "whitespace character");
   printf("\n%s%s%s", "vertical tab", isspace('\v') ? " is a " : " is not a ", "whitespace character");
   printf("\n%s%s%s", "percetage", isspace('\%') ? " is a " : " is not a ", "whitespace character");

 return 0;
}

Output

Demonstration of isspace():

for feed is a whitespace character
vertical tab is a whitespace character
percentage is not a whitespace character

Explanation
In the above example, standard character library function isspace determines whether its argument is a whitespace character or not.

Conditional operator ?: does the selection between two strings "is a" and "is not a".

This can be simply done with the help of isspace and if...else statement.

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

int main()
{
   printf("Demonstration of isspace( ):\n");

   if( isspace('\f') == 0)
     printf("\nform feed is not whitespace character");
   else
     printf("\nform feed is a whitespace character");

   if( isspace('\v') == 0)
     printf("\nvertical tab is not whitespace character");
   else
     printf("\nvertical tab is a whitespace character");

   if( isspace('\%') == 0)
     printf("\npercentage is not whitespace character");
   else
     printf("\npercentage is a whitespace character");

 return 0;
}

Output

Demonstration of isspace():

for feed is a whitespace character
vertical tab is a whitespace character
percentage is not a whitespace character

Explanation
In this example, we have used if...else statements instead of a conditional operator.

Here, if...else statement does the selection between two strings.