Name

isspace

Synopsis

Ascertains whether a given character produces space

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

The function isspace() tests whether its character argument produces whitespace rather than a glyph when printed—such as a space, tabulator, newline, or the like. If the argument is a whitespace character, isspace() returns a nonzero value (that is, true); if not, the function returns 0 (false).

Which characters fall into the whitespace class depends on the current locale setting for the category LC_CTYPE, which you can query or change using the setlocale() function. In the default locale C, the isspace() function returns true for the characters in Table 17-3.

Table 17-3. Whitespace characters in the default locale, C

Character

ASCII name

Decimal value

'\t’

Horizontal tabulator

9

'\n’

Line feed

10

'\v’

Vertical tabulator

11

'\f’

Page feed

12

'\r’

Carriage return

13

' '

Space

32

Example

char buffer[1024];
char *ptr = buffer;

while ( fgets( buffer, sizeof(buffer), stdin ) != NULL )
{
  ptr = buffer;
  while (isspace( *ptr ))             // Skip over leading whitespace.
    ptr++;
  printf( "The line read: %s\n", ptr );
}

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

See Also

The C99 function isblank(), which returns true for the space and horizontal tab characters; the corresponding C99 functions for wide characters, iswspace() and iswblank(); isalnum(), isalpha(), iscntrl(), isdigit(), isgraph(), islower(), isprint(), ispunct(), 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.