Printable ASCII characters list and how to check them in C
By Flavio Copes
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
| DEC | HEX | CHARACTER |
|---|---|---|
| 32 | 0x20 | <SPACE> |
| 33 | 0x21 | ! |
| 34 | 0x22 | ” |
| 35 | 0x23 | # |
| 36 | 0x24 | $ |
| 37 | 0x25 | % |
| 38 | 0x26 | & |
| 39 | 0x27 | ’ |
| 40 | 0x28 | ( |
| 41 | 0x29 | ) |
| 42 | 0x2A | * |
| 43 | 0x2B | + |
| 44 | 0x2C | , |
| 45 | 0x2D | - |
| 46 | 0x2E | . |
| 47 | 0x2F | / |
| 48 | 0x30 | 0 |
| 49 | 0x31 | 1 |
| 50 | 0x32 | 2 |
| 51 | 0x33 | 3 |
| 52 | 0x34 | 4 |
| 53 | 0x35 | 5 |
| 54 | 0x36 | 6 |
| 55 | 0x37 | 7 |
| 56 | 0x38 | 8 |
| 57 | 0x39 | 9 |
| 58 | 0x3A | : |
| 59 | 0x3B | ; |
| 60 | 0x3C | < |
| 61 | 0x3D | = |
| 62 | 0x3E | > |
| 63 | 0x3F | ? |
| 64 | 0x40 | @ |
| 65 | 0x41 | A |
| 66 | 0x42 | B |
| 67 | 0x43 | C |
| 68 | 0x44 | D |
| 69 | 0x45 | E |
| 70 | 0x46 | F |
| 71 | 0x47 | G |
| 72 | 0x48 | H |
| 73 | 0x49 | I |
| 74 | 0x4A | J |
| 75 | 0x4B | K |
| 76 | 0x4C | L |
| 77 | 0x4D | M |
| 78 | 0x4E | N |
| 79 | 0x4F | O |
| 80 | 0x50 | P |
| 81 | 0x51 | Q |
| 82 | 0x52 | R |
| 83 | 0x53 | S |
| 84 | 0x54 | T |
| 85 | 0x55 | U |
| 86 | 0x56 | V |
| 87 | 0x57 | W |
| 88 | 0x58 | X |
| 89 | 0x59 | Y |
| 90 | 0x5A | Z |
| 91 | 0x5B | [ |
| 92 | 0x5C | \ |
| 93 | 0x5D | ] |
| 94 | 0x5E | ^ |
| 95 | 0x5F | _ |
| 96 | 0x60 | ` |
| 97 | 0x61 | a |
| 98 | 0x62 | b |
| 99 | 0x63 | c |
| 100 | 0x64 | d |
| 101 | 0x65 | e |
| 102 | 0x66 | f |
| 103 | 0x67 | g |
| 104 | 0x68 | h |
| 105 | 0x69 | i |
| 106 | 0x6A | j |
| 107 | 0x6B | k |
| 108 | 0x6C | l |
| 109 | 0x6D | m |
| 110 | 0x6E | n |
| 111 | 0x6F | o |
| 112 | 0x70 | p |
| 113 | 0x71 | q |
| 114 | 0x72 | r |
| 115 | 0x73 | s |
| 116 | 0x74 | t |
| 117 | 0x75 | u |
| 118 | 0x76 | v |
| 119 | 0x77 | w |
| 120 | 0x78 | x |
| 121 | 0x79 | y |
| 122 | 0x7A | z |
| 123 | 0x7B | { |
| 124 | 0x7C | | |
| 125 | 0x7D | } |
| 126 | 0x7E | ~ |
Want me to talk about your product? You can sponsor this site.
Related posts about computers: