A computer does not store the letter A. It stores a number, and everyone agrees that this particular number means A. The ASCII standard is that agreement. It gives every common character a number, so text can be stored and moved between programs.
This note explains ASCII, shows how the word Apple becomes numbers, and uses %c to turn those numbers back into characters. It follows Char Data Type, which explains the char type itself.
What ASCII means
ASCII stands for American Standard Code for Information Interchange. The American National Standards Institute, the same body behind ANSI C, published it.
ASCII defines 128 codes, numbered 0 to 127. Each code stands for one character. The characters include:
- Uppercase letters
AtoZ. - Lowercase letters
atoz. - Digits
0to9. - Punctuation and symbols such as space,
!,#,&,", and@. - Control codes for keys such as Backspace, Tab, Return, and Escape.
To store any one of these codes, you need 7 bits. The next section shows why.
Why 7 bits are enough
| Symbol | Meaning |
|---|---|
| The number of choices for one bit, namely 0 or 1 | |
| The number of bit positions in one ASCII code | |
| The total number of different patterns that 7 bits can make | |
| The number of codes in ASCII, so the number of characters it can represent |
In plain words, each new bit doubles the number of patterns. Seven bits therefore give 128 different patterns, which is exactly enough for the 128 ASCII codes.
Check it by doubling one bit at a time.
| Bits available | Patterns available |
|---|---|
| 1 | 2 |
| 2 | 4 |
| 3 | 8 |
| 4 | 16 |
| 5 | 32 |
| 6 | 64 |
| 7 | 128 |
Written out, that is seven twos multiplied together, which also gives 128. Since the codes are numbered from 0 to 127, the highest code is 127 and the total is 128.
The word Apple becomes numbers
Take the word Apple. Each letter has its own code.
| Letter | ASCII code | Binary, 7 bits |
|---|---|---|
| A | 65 | 1000001 |
| p | 112 | 1110000 |
| p | 112 | 1110000 |
| l | 108 | 1101100 |
| e | 101 | 1100101 |
The machine stores those numbers, not the shapes of the letters. When a program prints the numbers as characters, the display turns each code back into its letter.
Here is how the code for A is built from bits.
Bit position: 7 6 5 4 3 2 1 0
Weight: 128 64 32 16 8 4 2 1
'A' = 0 1 0 0 0 0 0 1
↓ ↓
64 + 1 = 65
Legend: each column is one bit position. The Weight row says what a 1 in that position is worth. A 1 means add that weight. A 0 means skip it. The row for 'A' has a 1 under 64 and a 1 under 1, so the stored value is 65. The leading bit 7 is 0 because ASCII needs only 7 bits.
Storing the codes in C
You can write the numbers directly into char variables and print them as characters.
#include <stdio.h>
int main() {
char a1 = 65;
char a2 = 112;
char a3 = 112;
char a4 = 108;
char a5 = 101;
printf("%c%c%c%c%c\n", a1, a2, a3, a4, a5);
return 0;
}AppleFollow the program step by step.
char a1 = 65;creates a one-byte variable and stores the number 65.- The other four variables store 112, 112, 108, and 101.
printfreads each variable in order.- The format specifier
%ctellsprintfto treat each number as a character code. %c%c%c%c%cprints the five characters with no spaces between them, so the output isApple.
If you use %d instead, printf treats the same value as an integer and prints the number.
#include <stdio.h>
int main() {
char a1 = 'A';
printf("As a character: %c\n", a1);
printf("As a number: %d\n", a1);
return 0;
}As a character: A
As a number: 65The variable holds one byte either way. The format specifier only decides how to show that byte.
The shortcut: character constants
Writing ASCII codes by hand is slow and easy to get wrong. C lets you write the character itself in single quotes.
#include <stdio.h>
int main() {
char a1 = 'A';
char a2 = 'p';
char a3 = 'p';
char a4 = 'l';
char a5 = 'e';
printf("The string is: %c%c%c%c%c\n", a1, a2, a3, a4, a5);
return 0;
}The string is: AppleThe compiler replaces 'A' with 65, 'p' with 112, and so on. To you the program reads like the word. To the machine it is still a row of numbers.
Important
Single quotes make one character constant, as in
'A'. Double quotes make a string, as in"A". A string can hold more than one character, and it is stored differently. Strings and arrays come later.
When you reach arrays and strings, all five variables can be replaced by one line:
char word[] = "Apple";That line still stores the same ASCII codes, followed by a special 0 code that marks the end of the string.
The ASCII table at a glance
The 128 codes are grouped in a useful order.
| Codes | Count | What they are | Examples |
|---|---|---|---|
| 0 to 31 | 32 | Control codes | NUL 0, Backspace 8, Tab 9, Line feed 10, Return 13, Escape 27 |
| 32 to 47 | 16 | Space and punctuation | space 32, ! 33, " 34, # 35 |
| 48 to 57 | 10 | Digits | 0 48, 9 57 |
| 58 to 64 | 7 | Punctuation | : 58, @ 64 |
| 65 to 90 | 26 | Uppercase letters | A 65, Z 90 |
| 91 to 96 | 6 | Punctuation | [ 91, backtick 96 |
| 97 to 122 | 26 | Lowercase letters | a 97, z 122 |
| 123 to 126 | 4 | Punctuation | { 123, ~ 126 |
| 127 | 1 | Delete | DEL 127 |
Two patterns are worth remembering.
- The digits start at 48. So
'0'is 48,'1'is 49, and'9'is 57. - Each lowercase letter is 32 higher than its uppercase partner.
'A'is 65 and'a'is 97.'Z'is 90 and'z'is 122.
Do not confuse the digit '0' with the number 0. The character '0' has ASCII code 48. The character '1' has code 49. They are text characters, not the values zero and one.
Control characters
The first 32 codes do not print as symbols. They stand for actions and keys.
| Code | Name | C escape | What it does |
|---|---|---|---|
| 0 | NUL | \0 | Marks the end of a C string |
| 7 | BEL | \a | Sounds the bell on many terminals |
| 8 | BS | \b | Moves the cursor back one position |
| 9 | HT | \t | Moves to the next horizontal tab stop |
| 10 | LF | \n | Moves to the next line |
| 13 | CR | \r | Returns the cursor to the start of the line |
| 27 | ESC | none | Starts many terminal commands |
These are the codes behind the escape sequences described in Escape Sequences. When you write \n in a string, the compiler stores code 10. When the text is printed, the terminal moves to the next line instead of showing a symbol.
Full printable ASCII table
The table below covers codes 32 to 126, which are the printable characters, plus code 127, which is Delete. Read it across each row.
| Dec | Char | Dec | Char | Dec | Char | Dec | Char |
|---|---|---|---|---|---|---|---|
| 32 | (space) | 56 | 8 | 80 | P | 104 | h |
| 33 | ! | 57 | 9 | 81 | Q | 105 | i |
| 34 | ” | 58 | : | 82 | R | 106 | j |
| 35 | # | 59 | ; | 83 | S | 107 | k |
| 36 | $ | 60 | < | 84 | T | 108 | l |
| 37 | % | 61 | = | 85 | U | 109 | m |
| 38 | & | 62 | > | 86 | V | 110 | n |
| 39 | ’ | 63 | ? | 87 | W | 111 | o |
| 40 | ( | 64 | @ | 88 | X | 112 | p |
| 41 | ) | 65 | A | 89 | Y | 113 | q |
| 42 | * | 66 | B | 90 | Z | 114 | r |
| 43 | + | 67 | C | 91 | [ | 115 | s |
| 44 | , | 68 | D | 92 | (backslash) | 116 | t |
| 45 | - | 69 | E | 93 | ] | 117 | u |
| 46 | . | 70 | F | 94 | ^ | 118 | v |
| 47 | / | 71 | G | 95 | _ | 119 | w |
| 48 | 0 | 72 | H | 96 | (backtick) | 120 | x |
| 49 | 1 | 73 | I | 97 | a | 121 | y |
| 50 | 2 | 74 | J | 98 | b | 122 | z |
| 51 | 3 | 75 | K | 99 | c | 123 | { |
| 52 | 4 | 76 | L | 100 | d | 124 | | |
| 53 | 5 | 77 | M | 101 | e | 125 | } |
| 54 | 6 | 78 | N | 102 | f | 126 | ~ |
| 55 | 7 | 79 | O | 103 | g | 127 | DEL |
How ASCII fits inside a char
A char is one byte, which is 8 bits on most machines. ASCII needs only 7 bits, so the eighth bit is left at 0. The byte for 'A' looks like this.
Bit: 7 6 5 4 3 2 1 0
0 1 0 0 0 0 0 1
↑ └─────────────┬──────────┘
always 0 the 7 ASCII bits
The same byte written in hexadecimal is 0x41, because 65 decimal equals 0x41. This is the same byte you saw next to the letter 'D' in Address of a variable.
Note
You may see tables called “extended ASCII” with codes from 128 to 255. Those are not part of standard ASCII. They are separate 8-bit tables that add accented letters and other symbols. Standard ASCII stops at 127.
Summary
- ASCII gives 128 characters the codes 0 to 127.
- Seven bits are enough because .
- A
charstores the code, not the shape of the letter. %cprints a stored code as a character.%dprints it as a number.- Single quotes let you write the character, and the compiler fills in its code.
- Uppercase and lowercase letters are 32 apart, and the digits start at 48.
Previous: Char Data Type