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 A to Z.
  • Lowercase letters a to z.
  • Digits 0 to 9.
  • 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

SymbolMeaning
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 availablePatterns available
12
24
38
416
532
664
7128

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.

LetterASCII codeBinary, 7 bits
A651000001
p1121110000
p1121110000
l1081101100
e1011100101

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;
}
Apple

Follow the program step by step.

  1. char a1 = 65; creates a one-byte variable and stores the number 65.
  2. The other four variables store 112, 112, 108, and 101.
  3. printf reads each variable in order.
  4. The format specifier %c tells printf to treat each number as a character code.
  5. %c%c%c%c%c prints the five characters with no spaces between them, so the output is Apple.

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:    65

The 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: Apple

The 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.

CodesCountWhat they areExamples
0 to 3132Control codesNUL 0, Backspace 8, Tab 9, Line feed 10, Return 13, Escape 27
32 to 4716Space and punctuationspace 32, ! 33, " 34, # 35
48 to 5710Digits0 48, 9 57
58 to 647Punctuation: 58, @ 64
65 to 9026Uppercase lettersA 65, Z 90
91 to 966Punctuation[ 91, backtick 96
97 to 12226Lowercase lettersa 97, z 122
123 to 1264Punctuation{ 123, ~ 126
1271DeleteDEL 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.

CodeNameC escapeWhat it does
0NUL\0Marks the end of a C string
7BEL\aSounds the bell on many terminals
8BS\bMoves the cursor back one position
9HT\tMoves to the next horizontal tab stop
10LF\nMoves to the next line
13CR\rReturns the cursor to the start of the line
27ESCnoneStarts 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.

DecCharDecCharDecCharDecChar
32(space)56880P104h
33!57981Q105i
34”58:82R106j
35#59;83S107k
36$60<84T108l
37%61=85U109m
38&62>86V110n
39’63?87W111o
40(64@88X112p
41)65A89Y113q
42*66B90Z114r
43+67C91[115s
44,68D92(backslash)116t
45-69E93]117u
46.70F94^118v
47/71G95_119w
48072H96(backtick)120x
49173I97a121y
50274J98b122z
51375K99c123{
52476L100d124|
53577M101e125}
54678N102f126~
55779O103g127DEL

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 char stores the code, not the shape of the letter.
  • %c prints a stored code as a character. %d prints 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