Rewrite C Function: isspace

Last Updated : 10 July, 2022 · 2 min read

articles banner image

Today we are going to overview isspace function and its workings in C programming language. This tutorial is split up into three following parts:

Structure of isspace:

Declaration: int isspace(int c);

Return: non-zero value if int c is a whitespace value. Otherwise, 0.

Parameter list:
(int c) - the character that is checked to determine the return value.

Header file: <ctype.h>.

1. So, what is the isspace function in C?

This function in C language checks if the passed char value is a white-space character. The return value of isspace is an int.

If the argument is a white-space value, function isspace returns non-zero value.

If the argument is not a white-space value, the isspace function returns 0.

The isspace function is defined in the <ctype.h> library. To use this function, we have to include this header file into the C program (e.g. #include <ctype.h> ).

Example 1: Usage of the isspace function in C.

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

int main( void )
{
    
printf ( "%d", isspace ( '\n' ));
    
printf ( "%d", isspace ( '1' ));
    
return ( 0 );
}

Possible output:

1
0

2. And how to rewrite the isspace function in C?

Consider the code snippet below for a second:

Example 2: Rewritten isspace function in C

int our_isspace ( int c )
{
    
if ( ( c >= 9 && c <= 13 ) || ( c == 32 ) )
        
return ( 1 );
    
return ( 0 );
}

The our_isspace function above takes one parameter. Since this parameter c is of int type, arguments of char type (when sent as arguments to this function) get implicitly typecasted to an according numerical value.

On the line 3 we are checking if the received value of c is in the range of [9,13] or 32. If this conditional statement returns true, it means that the value is a white-space character (' ', '\n', '\r', '\t', '\v', 'f') and 1 is returned. Otherwise 0 is returned, indicating that the value of c is not a white-space character.

3. Quiz of the reader's knowledge

Quiz : Rewrite C Function

1What is the return value of isspace(13)?

2What is the return value of isspace('\n')?

3In which library is isspace defined?

quiz completed!

Congrats!

Feel free to share your achievement below!