Name
isspace
Synopsis
Ascertains whether a given character produces space
#include <ctype.h> intisspace
( intc
);
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.
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.