Printable ASCII characters list and how to check them in C

By

Printable ASCII characters run from 32 to 126, including space. See the decimal and hex table, and learn how to check characters in C with isprint.

~~~

Printable ASCII is the range from decimal 32 to 126. That is space through tilde. Codes 0–31 and DEL (127) are non-printable control characters, not printable text.

There are 95 printable ASCII characters, including the space. A newline (\n) or tab (\t) changes how text is displayed, but neither is a printable ASCII character.

Printable characters in C

Use isprint() from <ctype.h> to check whether a character is printable. It returns a nonzero value for a printable character, and zero otherwise:

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

int main(void) {
  printf("A: %d\n", isprint('A') != 0);
  printf("Space: %d\n", isprint(' ') != 0);
  printf("Newline: %d\n", isprint('\n') != 0);
  return 0;
}

This prints 1, 1, and 0. The comparison with zero gives us a consistent 0 or 1, because isprint() does not promise to return exactly 1 for a match.

When checking a value stored in a char, cast it to unsigned char:

char character = 'A';
if (isprint((unsigned char)character)) {
  puts("Printable");
}

Passing a negative value other than EOF to isprint() causes undefined behavior. The cast avoids that problem when char is signed. If you read with getchar(), keep its result in an int so you can check for EOF before converting it.

isprint() follows the current locale. In the default C locale on an ASCII-compatible system, printable characters are the range below. If you specifically need printable ASCII bytes, test byte >= 32 && byte <= 126. Neither that test nor calling isprint() on individual bytes tells you whether a UTF-8 character is printable.

See the C library character classification documentation for the locale and argument rules.

Printable ASCII table

DECHEXCHARACTER
320x20<SPACE>
330x21!
340x22”
350x23#
360x24$
370x25%
380x26&
390x27’
400x28(
410x29)
420x2A*
430x2B+
440x2C,
450x2D-
460x2E.
470x2F/
480x300
490x311
500x322
510x333
520x344
530x355
540x366
550x377
560x388
570x399
580x3A:
590x3B;
600x3C<
610x3D=
620x3E>
630x3F?
640x40@
650x41A
660x42B
670x43C
680x44D
690x45E
700x46F
710x47G
720x48H
730x49I
740x4AJ
750x4BK
760x4CL
770x4DM
780x4EN
790x4FO
800x50P
810x51Q
820x52R
830x53S
840x54T
850x55U
860x56V
870x57W
880x58X
890x59Y
900x5AZ
910x5B[
920x5C\
930x5D]
940x5E^
950x5F_
960x60`
970x61a
980x62b
990x63c
1000x64d
1010x65e
1020x66f
1030x67g
1040x68h
1050x69i
1060x6Aj
1070x6Bk
1080x6Cl
1090x6Dm
1100x6En
1110x6Fo
1120x70p
1130x71q
1140x72r
1150x73s
1160x74t
1170x75u
1180x76v
1190x77w
1200x78x
1210x79y
1220x7Az
1230x7B{
1240x7C|
1250x7D}
1260x7E~
Tagged: Computer · All topics

Want me to talk about your product? You can sponsor this site.

~~~

Related posts about computers: