ASCII (American Standard Code for Information Interchange) assigns a unique integer value to every character. In C++, a character can be directly converted to its corresponding ASCII value using type conversion.
- Every character has a unique ASCII value.
- Can be directly converted to their integer ASCII values.

Example: The following program prints the ASCII value of a character.
#include <iostream>
using namespace std;
int main() {
char ch = 'A';
cout << "Character: " << ch << endl;
cout << "ASCII Value: " << int(ch);
return 0;
}
Output
Character: A ASCII Value: 65
Working of ASCII Conversion
A character in C++ is internally stored as an integer value based on the ASCII encoding.
- int(ch) explicitly converts the character into its ASCII value.
- The conversion takes place automatically because char is an integral data type.
- The operation requires constant time and does not modify the original character.
Print ASCII Value of Every Character in a String
We can iterate through a string and convert each character into its ASCII value.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "GeeksforGeeks";
cout << "Character\tASCII Value\n";
for (char ch : str) {
cout << ch << "\t\t" << int(ch) << endl;
}
return 0;
}
Output
Character ASCII Value G 71 e 101 e 101 k 107 s 115 f 102 o 111 r 114 G 71 e 101 e 101 k 107 s 115
Explanation
- The loop visits each character in the string and converts it to its corresponding ASCII value using int(ch).
- Both the character and its ASCII value are printed for every iteration.
Print ASCII Values of Multiple Characters
The following program prints the ASCII values of uppercase English letters.
#include <iostream>
using namespace std;
int main() {
cout << "Character\tASCII Value\n";
for (char ch = 'A'; ch <= 'Z'; ch++) {
cout << ch << "\t\t" << int(ch) << endl;
}
return 0;
}
Output
Character ASCII Value A 65 B 66 C 67 D 68 E 69 F 70 G 71 H 72 I 73 J 74 K 75 L 76 M 77 N 78 O 79 P 80 Q 81 R 82 S 83 T 84 U 85 V 86 W 87 X 88 Y 89 Z 90
Explanation
- The loop iterates from 'A' to 'Z'.
- Each character is converted into its ASCII value before printing.
Time Complexity
| Operation | Time Complexity |
|---|---|
| Print ASCII of one character | O(1) |
| Print ASCII of every character in a string | O(n) |
| Print ASCII values of uppercase letters | O(1) (26 iterations) |
Applications
Printing ASCII values is useful in many programming tasks.
- Converting characters into numeric values for processing.
- Comparing characters using their ASCII codes.
- Implementing simple encoding or encryption algorithms.
- Debugging character input and text-processing programs.
Best Practices
Follow these practices while working with ASCII values.
- Use int(ch) for explicit and readable conversions.
- Use unsigned char when working with extended character sets.
- Prefer Unicode-aware libraries when international characters are required.
- Remember that standard ASCII represents only 128 characters.