Skip to content
Tutorials Center
My learning

Topics

C Reference ยท ctype.h, math.h and others

C ispunct(), isprint() and iscntrl() functions

Reference for C ispunct(), isprint(), isgraph() and iscntrl() from ctype.h: which ASCII characters belong to each class and how to sanitize text.

These four classifiers in <ctype.h> split characters by how they look when printed. isprint() is true for characters that take up a position on the screen (including the space), isgraph() for printable characters except the space, ispunct() for printable characters that are neither letters, digits nor space, and iscntrl() for control characters such as newline, tab and the null byte.

They are useful for sanitizing text before showing it, for building hex dumps, and for parsing tokens.

C
#include <ctype.h>

int ispunct(int c);
int isprint(int c);
int isgraph(int c);
int iscntrl(int c);

Try it

C Classes of the first ASCII characters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Sanitizing text and a hex dump

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
int ispunct(int c);
int isprint(int c);
int isgraph(int c);
int iscntrl(int c);
Name Description
c A value representable as unsigned char, or EOF.

Returns: non-zero if c is in the class, otherwise zero.

Errors / undefined behavior: values outside the unsigned char range (other than EOF) are undefined, so cast char arguments.

Usage notes

  • ASCII layout in the "C" locale.
    • Control characters: codes 0 to 31 and 127 (DEL). iscntrl is true for these.
    • Printable: codes 32 to 126. isprint is true for these, isgraph for 33 to 126.
    • Punctuation: the 32 symbols of ASCII that are not letters, digits or space (such as !, #, @, [, _, ~). ispunct is true for them.
  • Relationships. isgraph(c) equals isprint(c) && c != ' '. ispunct(c) equals isgraph(c) && !isalnum(c). The space is printable but neither graphical nor punctuation. A character is never both printable and control.
  • Locales. Other locales may classify high-bit single-byte characters as printable letters; in the "C" locale all bytes above 127 are in none of these classes. UTF-8 multi-byte characters appear as several such bytes, so a byte-wise isprint will reject non-ASCII text. Use the wide variants (iswprint) or a Unicode library if you need international text.
  • Cast char first: isprint((unsigned char)ch).
  • Output safety. Printing raw untrusted bytes can send terminal escape sequences (code 27, ESC). Replacing everything for which iscntrl is true, except newline and tab where you want them, is a simple first defense.
  • Not a security filter on its own. Checking isprint does not make a string safe for shell commands, SQL or HTML.
  • Don't pass expressions with side effects, as the function might be a macro.
  • Standard version. All four are in C89.