isprint()

Updated: May 06, 2022

Test a character to see if it's any printable character, including a space

Synopsis:

#include <ctype.h>

int isprint( int c );

Arguments:

c
The character you want to test. This must be representable as an unsigned char or be EOF; the behavior for other values is undefined. Because this argument is interpreted as an int, to avoid sign extension on character values greater than 0x7F, you must cast the argument to the unsigned data type; otherwise, the function will behave unpredictably.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The isprint() function tests for any printable character, including a space (' '). The isgraph() function is similar, except that it excludes the space character from the character set being tested.

Returns:

Nonzero if c is a printable character; otherwise, zero.

Examples:

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

char the_chars[] = { 'A', 0x09, ' ', 0x7d };

#define SIZE sizeof( the_chars ) / sizeof( char )

int main( void )
{
    int i;

    for( i = 0; i < SIZE; i++ ) {
        if( isprint( (unsigned)the_chars[i] ) ) {
            printf( "Char %c is a printable character\n",
                the_chars[i] );
        } else {
            printf( "Char %c is not a printable character\n",
                the_chars[i] );
        }
    }
    
    return EXIT_SUCCESS;
}

produces the output:

Char A is a printable character
Char     is not a printable character
Char   is a printable character
Char } is a printable character

Classification:

ANSI, POSIX 1003.1

Safety:  
Cancellation point No
Interrupt handler Yes
Signal handler Yes
Thread Yes