C++ Program To Print ASCII Value of a Character

Last Updated : 20 Jul, 2026

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.

C++ Program To Print ASCII Value of a Character

Example: The following program prints the ASCII value of a character.

C++
#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.

We can iterate through a string and convert each character into its ASCII value.

C++
#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.

The following program prints the ASCII values of uppercase English letters.

C++
#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

OperationTime Complexity
Print ASCII of one characterO(1)
Print ASCII of every character in a stringO(n)
Print ASCII values of uppercase lettersO(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.
Comment