Name

isblank

Synopsis

Ascertains whether a given character is a space or tab character

#include <ctype.h>
intisblank( int c );

The function isblank() is a recent addition to the C character type functions. It returns a nonzero value (that is, true) if its character argument is either a space or a tab character. If not, the function returns 0 (false).

Example

This program trims trailing blank characters from the user’s input:

#define MAX_STRING 80

char raw_name[MAX_STRING];
int i;

printf( "Enter your name, please: " );
fgets( raw_name, sizeof(raw_name), stdin );

/* Trim trailing blanks: */

i = ( strlen(raw_name) − 1 );   // Index the last character.
while ( i >= 0 )                // Index must not go below first character.
{
  if (  raw_name[i] == '\n' )
    raw_name[i] = '\0';         // Chomp off the newline character.
  else if (isblank( raw_name[i] ) )
    raw_name[i] = '\0';         // Lop off trailing spaces and tabs.
  else
    break;                      // Real data found; stop truncating.
  --i;                          // Count down.
}

See also the example for isprint() in this chapter.

See Also

The corresponding C99 function for wide characters, iswblank(); isalnum(), isalpha(), iscntrl(), isdigit(), isgraph(), islower(), isprint(), ispunct(), isspace(), isupper(), isxdigit()

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.