char stores a character code. ASCII assigns values 0–127 to common English letters, digits, symbols, and control characters.
Converting between characters and integers
Convert a character to an integer to inspect its code, or convert a value from 0–127 to char to obtain the matching ASCII character. A digit character is not the number itself: '7' - '0' produces integer 7.
#include <iostream>
using namespace std;
int main() {
char letter = 'A';
int code = static_cast<unsigned char>(letter);
char next = static_cast<char>(code + 1);
cout << letter << " = " << code << ", next = " << next << endl;
return 0;
}INTERACTIVE LAB
Complete interactive ASCII atlas
ACCESSFREE
Select any value from 0–127 to inspect its character, decimal, hexadecimal, and binary representations.
Traversing ASCII
Control characters have no visible glyph, so a program should label values 0–31 and 127 separately.
#include <iostream>
using namespace std;
int main() {
for (int value = 0; value <= 127; value++) {
if (value >= 32 && value <= 126) {
cout << value << " " << static_cast<char>(value) << endl;
} else {
cout << value << " control" << endl;
}
}
return 0;
}KNOWLEDGE QUIZ
QUESTION
What is the decimal ASCII value of 'A'?
HANDS-ON PRACTICE
Build an ASCII classifier
Read one ASCII character and classify it as uppercase, lowercase, digit, whitespace, or other.
- Read one character and keep its integer code.
- Use character ranges for digits and English letters.
- Handle space, tab, and newline as whitespace and print the category.