ASCII Value of a Character: C, C++, Java, Python
Programs to find the ASCII value of a character in C, C++, Java, and Python. Key ASCII ranges, worked examples, and interview variants for placement tests.
A character variable in C, C++, Java, or Python stores an integer, not the letter itself: 'A' stores 65, 'S' stores 83, and 's' stores 115.
Those integers are ASCII values. The ASCII standard (American Standard Code for Information Interchange) maps 128 characters to integers 0 through 127. The table covers control codes, digits, uppercase and lowercase letters, and common punctuation. Knowing the key ranges is a prerequisite for character-classification problems, which appear in AMCAT, CoCubes, and on-campus placement coding rounds.
What Is an ASCII Value?
ASCII is a 7-bit encoding: 128 code points, each an integer from 0 to 127. The entries split into three practical groups:
- Control codes (0–31 and 127): non-printable instructions such as newline (
10), horizontal tab (9), and carriage return (13). These appear in input-parsing problems more than in ASCII-value questions. - Printable ASCII (32–126): the characters you type. Space is 32, digits start at 48, uppercase letters at 65, lowercase at 97.
- Extended ASCII (128–255): vendor-specific extensions not covered by the 7-bit standard. Portable programs avoid relying on these.
The value stored in a char variable is exactly one of these integers. Print that integer and you have the ASCII value. Print the variable as a character and you see the letter.
How Each Language Reads the Integer
C
A char in C holds a small integer (typically 1 byte). Passing it to printf with the %d format specifier prints the integer directly:
char ch = 'S';
printf("%d\n", ch); /* prints 83 */
No cast is needed. %c prints the character S; %d prints the integer 83. The same variable serves both purposes without any conversion.
C++
C++ inherits char from C. Streams treat char as a character by default, so the explicit (int) cast makes the numeric intent clear:
char ch = 'S';
cout << (int)ch << endl; // prints 83
Without (int), cout << ch outputs S rather than 83.
Java
Java’s char is a 16-bit unsigned integer storing a UTF-16 code unit. For standard ASCII characters (code points 0–127), the numeric value matches the ASCII table exactly. An explicit cast converts char to int:
char ch = 'S';
int ascii = (int) ch; // 83
System.out.println(ascii);
Implicit widening also works: int ascii = ch; compiles without a cast in Java.
Python
Python has no char type. A one-character string holds the character. The built-in ord() function returns the Unicode code point, which equals the ASCII value for characters in the 0–127 range:
print(ord('S')) # 83
The inverse is chr(): chr(83) returns 'S'. This pair appears often in string-transformation and encoding questions.
Programs in C and C++
C Program
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
printf("ASCII value of '%c' is %d\n", ch, ch);
return 0;
}
Traced outputs:
- Input
S:ASCII value of 'S' is 83 - Input
s:ASCII value of 's' is 115 - Input
A:ASCII value of 'A' is 65 - Input
0:ASCII value of '0' is 48
The %c and %d format specifiers read the same ch variable; the only difference is how printf interprets the integer it receives.
C++ Program
#include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
cout << "ASCII value of '" << ch << "' is " << (int)ch << endl;
return 0;
}
Traced output for input z: ASCII value of 'z' is 122
Programs in Java and Python
Java Program
import java.util.Scanner;
public class AsciiValue {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = sc.next().charAt(0);
int ascii = (int) ch;
System.out.println("ASCII value of '" + ch + "' is " + ascii);
}
}
sc.next()reads one whitespace-delimited token..charAt(0)picks the first character.(int) chwidens the 16-bitcharto a 32-bitint.- Traced output for
a:ASCII value of 'a' is 97 - Traced output for
Z:ASCII value of 'Z' is 90
Python Program
ch = input("Enter a character: ")
print(f"ASCII value of '{ch}' is {ord(ch)}")
- Traced output for
a:ASCII value of 'a' is 97 - Traced output for
9:ASCII value of '9' is 57
ord() raises a TypeError if the string is not exactly one character long. For production code, add a guard: if len(ch) != 1: raise ValueError("single character required"). The chr() inverse is chr(97) which returns 'a'.
Key ASCII Ranges for Placement Tests
| Character group | ASCII range | Example values |
|---|---|---|
| Control codes | 0–31, 127 | Newline = 10, Tab = 9 |
| Space | 32 | First printable character |
Digits '0' to '9' | 48–57 | '0' = 48, '9' = 57 |
Uppercase 'A' to 'Z' | 65–90 | 'A' = 65, 'Z' = 90 |
Lowercase 'a' to 'z' | 97–122 | 'a' = 97, 'z' = 122 |
The offset between corresponding uppercase and lowercase letters is exactly 32. 'a' (97) minus 'A' (65) = 32. This constant holds for all 26 letter pairs and drives the arithmetic case-conversion technique in the next section.
Interview Variants and Edge Cases
These three variants appear in AMCAT and on-campus placement coding rounds alongside programs like the sum of digits and replacing zeros with ones.
Print All Printable ASCII Characters in a Range
#include <stdio.h>
int main() {
int i;
for (i = 32; i <= 126; i++) {
printf("%d : %c\n", i, i);
}
return 0;
}
In Python:
for i in range(32, 127):
print(i, chr(i))
Both loops print each integer from 32 to 126 alongside its character. The upper bound is 126, not 127, because character 127 is the non-printable DEL control code.
Case Conversion Without Library Functions
Subtract 32 to convert lowercase to uppercase; add 32 to go the other direction. This works for all 26 letter pairs and uses no library calls:
char ch = 'a';
if (ch >= 'a' && ch <= 'z') {
char upper = ch - 32; /* 'A' */
printf("%c\n", upper);
}
In Python: chr(ord('a') - 32) returns 'A'. The space complexity of this operation is O(1); it holds a single integer at any point.
Check Whether a Character Is a Digit, Letter, or Special Symbol
Three range checks cover all categories:
- Is a digit: ASCII value is in 48–57 (i.e.,
ch >= '0' && ch <= '9'in C/C++/Java) - Is uppercase: ASCII value is in 65–90
- Is lowercase: ASCII value is in 97–122
- Is a special symbol: none of the three conditions above are true
In Python, ch.isdigit(), ch.isupper(), and ch.islower() use these same bounds internally. The explicit ASCII check appears in C placement questions where ctype.h is excluded from test constraints.
The ord() and chr() functions make it straightforward to test these conditions interactively. TinkerLLM provides a browser-based Python sandbox at ₹499 where you can inspect ASCII values and try the case-conversion arithmetic without installing a compiler. The ord()-plus-arithmetic pattern used for case conversion above extends directly to Caesar cipher and ROT13 challenges that appear in later placement rounds.
Primary sources
Frequently asked questions
What is the ASCII value of the letter A?
The ASCII value of uppercase 'A' is 65. The 26 uppercase letters run from 65 ('A') to 90 ('Z').
What is the ASCII value of a space character?
A space character has ASCII value 32. It is the first printable character in the ASCII table, sitting just above the control-code block that ends at 31.
How do you find the ASCII value of a character in Python?
Use the built-in ord() function: ord('A') returns 65, ord('a') returns 97. The reverse is chr(): chr(65) returns 'A'. Both are single-argument built-ins with no import required.
What is the difference between ASCII values of uppercase and lowercase letters?
Exactly 32. 'a' is 97 and 'A' is 65 (97 minus 65 = 32). This constant offset holds for all 26 letter pairs, making arithmetic case-conversion possible: add 32 to go lowercase, subtract 32 to go uppercase.
Can a char in C hold an ASCII value greater than 127?
A signed char holds values from -128 to 127; an unsigned char holds 0 to 255. For standard ASCII (0 to 127) both types work without issues. For extended ASCII characters above 127, declare the variable as unsigned char to avoid sign-extension surprises.
Why does Java use int for ASCII values instead of char?
Java's char type is a 16-bit UTF-16 code unit, not a byte. For standard ASCII characters (code points 0 to 127), the cast (int)ch gives the correct ASCII value. Java uses int to store the result because char arithmetic can produce unexpected results when values exceed 127, and int makes the numeric intent explicit.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹499)