Previous Page
Next Page

isprint

Ascertains whether a given character is printable

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

The isprint( ) function tests whether its argument is a printing character. If the argument is a printing character, isprint( ) returns a nonzero value (that is, TRue); if not, the function returns 0 (false).

"Printing" means only that the character occupies printing space on the output medium, not that it fills the space with a glyph. Thus the space is a printing character (isprint(' ') returns true), even though it does not leave a mark (isgraph(' ') returns false).

Which character codes represent printable characters 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 printable characters are the alphanumeric characters, the punctuation characters, and the space character; the corresponding character codes are those from 32 through 126.

Example

unsigned int c;

printf("\nThe current locale for the 'is ...' functions is '%s'.\n",
       setlocale(LC_CTYPE, NULL));

printf("Here is a table of the 'is ...' values for the characters"
       " from 0 to 127 in this locale:\n\n");

for ( c = 0; c < 128; c++ ) // Loop iteration for each table row.
{
  if ( c % 24 == 0 )        // Repeat table header every 24 rows.
  {
    printf("Code char alnum alpha blank cntrl digit graph lower"
           " print punct space upper xdigit\n");
    printf("---------------------------------------------------"
           "-------------------------------\n");
  }
  printf(  "%4u %4c %3c %5c %5c %5c %5c %5c %5c %5c %5c %5c %5c %5c\n",
           c,                              // Print numeric character code.
           ( isprint( c )  ?  c  : ' ' ),  // Print the glyph, or a space
                                           // if it's not printable.
           ( isalnum( c )  ? 'X' : '-' ),  // In a column for each category,
           ( isalpha( c )  ? 'X' : '-' ),  // print X for yes or - for no.
           ( isblank( c )  ? 'X' : '-' ),
           ( iscntrl( c )  ? 'X' : '-' ),
           ( isdigit( c )  ? 'X' : '-' ),
           ( isgraph( c )  ? 'X' : '-' ),
           ( islower( c )  ? 'X' : '-' ),
           ( isprint( c )  ? 'X' : '-' ),
           ( ispunct( c )  ? 'X' : '-' ),
           ( isspace( c )  ? 'X' : '-' ),
           ( isupper( c )  ? 'X' : '-' ),
           ( isxdigit( c ) ? 'X' : '-' ) );
}  // end of loop for each character value

The following selected lines from the table produced by this program include at least one member and one nonmember of each category:

Code char alnum alpha blank cntrl digit graph lower print punct space upper xdigit
----------------------------------------------------------------------------------
  31        -     -     -     X     -     -     -     -     -     -     -     -
  32        -     -     X     -     -     -     -     X     -     X     -     -
  33    !   -     -     -     -     -     X     -     X     X     -     -     -

  48    0   X     -     -     -     X     X     -     X     -     -     -     X

  65    A   X     X     -     -     -     X     -     X     -     -     X     X

 122    z   X     X     -     -     -     X     X     X     -     -     -     -

See Also

isgraph( ); the corresponding C99 function for wide characters, iswprint( ); isalnum( ), isalpha( ), isblank( ), iscntrl( ), isdigit( ), islower( ), ispunct( ), isspace( ), isupper( ), isxdigit( )


Previous Page
Next Page