Computers only understand numbers — so how do they store the letter “A” or the digit “5”? The answer is ASCII (American Standard Code for Information Interchange), a standard that assigns every common character a unique number. The letter A is 65, a is 97, the digit 0 is 48, and even the space you just typed is 32.
In this guide you’ll learn how ASCII works and — more importantly — how to write C programs that convert characters to ASCII values, convert numbers back to characters, and generate the complete ASCII table automatically.
What Is ASCII?
ASCII is a character encoding standard that maps 128 characters (values 0–127) to numbers. Each character gets a 7-bit code, which means every device — from a 1980s terminal to your phone — agrees that 65 means “A”.
The table is organized in useful blocks:
| Range | Contains |
|---|---|
| 0–31 | Control characters (newline, tab, null terminator) |
| 32 | Space (the first printable character) |
| 33–47 | Punctuation (!, ", #, …) |
| 48–57 | Digits 0–9 |
| 58–64 | More punctuation (:, ;, <, =, >, ?, @) |
| 65–90 | Uppercase letters A–Z |
| 91–96 | Brackets and symbols |
| 97–122 | Lowercase letters a–z |
| 123–126 | Braces, pipe, tilde |
| 127 | DEL (delete control character) |
A few values worth memorizing:
| Character | Decimal | Hex | Octal |
|---|---|---|---|
A | 65 | 41 | 101 |
Z | 90 | 5A | 132 |
a | 97 | 61 | 141 |
z | 122 | 7A | 172 |
0 | 48 | 30 | 60 |
9 | 57 | 39 | 71 |
| space | 32 | 20 | 40 |
newline (\n) | 10 | 0A | 12 |
Notice the pattern: uppercase letters run 65–90, lowercase 97–122 — exactly 32 apart. That’s why converting case in C is as simple as adding or subtracting 32.
A Quick Note on Extended ASCII
You’ll sometimes see “extended ASCII” tables going up to 255. Be careful: values 128–255 are not universal — they depend on the code page or encoding your system uses (Windows-1252, Latin-1, etc.). The programs below generate the standard 0–127 table, which is the same everywhere.
Program 1: Get the ASCII Value of Any Character
The core trick is simple: in C, a char is a small integer. Printing it with %d shows its numeric (ASCII) value.
#include <stdio.h>
int main(void)
{
char c;
printf("Enter any character: ");
scanf("%c", &c); /* read one character */
printf("The ASCII value of '%c' is %d\n", c, c);
/* %c prints the character, %d prints its numeric value */
return 0;
}
Sample output:
Enter any character: A
The ASCII value of 'A' is 65
Line-by-line:
#include <stdio.h>— brings inprintfandscanf.char c;— declares a variable to hold the character.scanf("%c", &c);— reads a single character from the keyboard. (&cpasses the variable’s address soscanfcan fill it.)printf("...%d...", c);— the%dformat specifier prints the character’s decimal value, i.e., its ASCII code. This is the whole trick: the same variable printed with%cshows the letter, with%dshows the number.
Program 2: Get the Character for Any Number
The reverse works too — read a number and print it with %c:
#include <stdio.h>
int main(void)
{
int n;
printf("Enter a number (0-127): ");
scanf("%d", &n); /* read a decimal number */
printf("The character for ASCII value %d is '%c'\n", n, n);
/* %c interprets the number as a character code */
return 0;
}
Sample output:
Enter a number (0-127): 97
The character for ASCII value 97 is 'a'
Program 3: Generate the Complete ASCII Table
Now combine the ideas with a loop to print the entire table — character, decimal, hexadecimal, and octal for every value:
#include <stdio.h>
int main(void)
{
int i;
printf("Char\tDec\tHex\tOct\n");
printf("----\t---\t---\t---\n");
for (i = 32; i <= 126; i++) /* printable range */
{
printf("%c\t%d\t%x\t%o\n", i, i, i, i);
/* %c = character, %d = decimal, %x = hexadecimal, %o = octal */
}
return 0;
}
Sample output:
Char Dec Hex Oct
---- --- --- ---
32 20 40
! 33 21 41
" 34 22 42
...
~ 126 7e 176
How it works:
- The loop runs from 32 to 126 — the printable range. (Values 0–31 are control characters that don’t display, and 127 is DEL; starting at 32 keeps the output clean.)
- Each iteration prints the same integer four ways:
%crenders it as a character,%das decimal,%xas hexadecimal,%oas octal. - To include the full 0–127 range, just change the loop to
for (i = 0; i <= 127; i++)— the control characters will show as blank or odd glyphs, which is expected.
How to Compile and Run
Save the program as ascii.c, then:
gcc ascii.c -o ascii
./ascii
gcc is preinstalled on Linux and macOS. On Windows, install MinGW-w64 or use WSL, then run the same commands.
Variations Worth Trying
- Uppercase to lowercase: since
'a' - 'A' == 32,c + 32converts any uppercase letter to lowercase (andc - 32goes the other way). - Digit to number: the character
'5'has ASCII value 53, so'5' - '0'gives the integer 5. This is how parsers convert digit characters to real numbers. - Sum check: loop over a string and add up the ASCII values — a classic beginner exercise for practicing loops and arrays.
ASCII vs Unicode
ASCII covers only 128 characters — enough for English, but not for the world’s languages. Unicode is the modern superset: its first 128 code points are identical to ASCII, then it extends to over 150,000 characters (emoji included). UTF-8, the dominant Unicode encoding on the web, stores plain ASCII text exactly as ASCII. So everything you learn here carries straight over.
Frequently Asked Questions
What is the ASCII value of ‘A’?
65. Lowercase ‘a’ is 97, and the digit ‘0’ is 48.
Why does %d print a number when I give it a character?
Because in C a char is stored as an integer. %d prints the stored number (the ASCII code); %c interprets that number as a character.
How many characters are in the standard ASCII table?
128 (values 0–127). The extended 256-value tables (0–255) are code-page dependent and not universal.
What’s the difference between ASCII and Unicode?
ASCII is a 128-character subset of Unicode. The first 128 Unicode code points match ASCII exactly, so ASCII text is valid Unicode/UTF-8 text.
Do I need conio.h or getch()?
No. Those come from old DOS-era compilers (Turbo C) and aren’t part of standard C. The programs above use only <stdio.h> and compile with any modern compiler.
Conclusion
ASCII is the simple agreement — 65 means “A” on every computer — that makes text work. And in C, working with it is nearly effortless: a char is already a number, so %d reveals the code and %c renders the character. With the table generator above, you can print the whole mapping in a dozen lines.
If you’re learning C, this is a great foundation — next, explore how C compares to other languages in C vs C++ and C vs Java.
Updated: October 2026 — fully rewritten and verified.
Vincent Cruz is the founder and editor of Durofy, where he covers business, technology, and practical consumer guides.







4 Comments
Thanks for the amazing comments.
All the best making your ASCII values and tables in C.
Pingback: URL
Thanks for reading :)
Guides us through the right ASCII values and provides good information.