Every character in C is stored as a small integer. For ordinary English letters, digits and symbols, that integer is the character’s ASCII value. So finding the ASCII value in C is easy: read the character into a char variable and print it with %d instead of %c.
What Is ASCII?
ASCII stands for American Standard Code for Information Interchange. It gives a number from 0 to 127 to 128 characters: English letters, digits, punctuation, the space, and some control characters such as newline.
| Character | ASCII value | Character | ASCII value |
|---|---|---|---|
| Space | 32 | A |
65 |
! |
33 | Z |
90 |
& |
38 | a |
97 |
0 |
48 | z |
122 |
9 |
57 | Newline (n) |
10 |
Two patterns are worth remembering: capital letters run from 65 to 90, and small letters from 97 to 122. Each small letter is exactly 32 more than its capital.
Program 1: Find the ASCII Value of a Character
#include <stdio.h>
int main(void) {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
printf("The ASCII value of %c is %dn", ch, ch);
return 0;
}
Output:
Enter a character: &
The ASCII value of & is 38
How it works
scanf(" %c", &ch)reads one character. The space before%cskips any leftover spaces or newlines from earlier input.- The same variable is printed twice:
%cshows it as a character, and%dshows the number stored inside it, which is the ASCII value.
Program 2: Find the Character from an ASCII Value
#include <stdio.h>
int main(void) {
int code;
printf("Enter an ASCII value (32 to 126): ");
scanf("%d", &code);
printf("The character for %d is %cn", code, code);
return 0;
}
Output:
Enter an ASCII value (32 to 126): 97
The character for 97 is a
Values 32 to 126 are the printable characters. Values 0 to 31 and 127 are control characters and do not show anything useful on screen.
Program 3: ASCII Values of All Characters in a String
#include <stdio.h>
int main(void) {
char str[] = "Hi!";
for (int i = 0; str[i] != ' '; i++) {
printf("%c = %dn", str[i], str[i]);
}
return 0;
}
Output:
H = 72
i = 105
! = 33
The loop stops at ' ', the null character with ASCII value 0, which marks the end of every C string.
Program 4: Print an ASCII Table
#include <stdio.h>
int main(void) {
for (int code = 65; code <= 70; code++) {
printf("%c = %dn", code, code);
}
return 0;
}
Output:
A = 65
B = 66
C = 67
D = 68
E = 69
F = 70
Change the loop to run from 32 to 126 to print every printable ASCII character.
Program 5: Convert Lowercase to Uppercase Using ASCII
#include <stdio.h>
int main(void) {
char ch = 'a';
if (ch >= 'a' && ch <= 'z') {
ch = ch - 32; /* 'a' (97) becomes 'A' (65) */
}
printf("%cn", ch);
return 0;
}
Output:
A
In real programs, the toupper() and tolower() functions from <ctype.h> do this more safely.
Common Mistakes
- Printing with
%cand expecting a number. Use%dfor the ASCII value. - Using
scanf("%c", ...)after anotherscanf. It may read the leftover newline (value 10). Add a space:" %c". - Confusing the digit ‘5’ with the number 5. The character
'5'has ASCII value 53. To get the number, writech - '0'. - Expecting ASCII values for symbols like ₹ or Hindi letters. These are not part of ASCII. They are stored in several bytes using Unicode (UTF-8).
Frequently Asked Questions
What is the ASCII value of A and a?
A is 65 and a is 97.
What is the ASCII value of 0?
The character '0' is 48. The null character ' ' is 0.
Which format specifier prints the ASCII value in C?
%d. It prints the integer stored in the char variable.
How many characters are there in ASCII?
128 (values 0 to 127). “Extended ASCII” tables use 128 to 255, but those characters differ from system to system.
Related: Reverse a string in C