ASCII Value of a Character
Beginner-friendly C++ program that takes a character as input and prints its ASCII value using type casting.
C++ ASCII Value of a Character Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}Enter a character: A
ASCII value of 'A' is: 65Understanding ASCII Value of a Character
This program teaches how characters are stored and handled inside the computer. Every character you type—like A, a, 5, #, !, or space—has its own unique number called an ASCII value. This program takes a single character from the user and prints its ASCII number.
---
1. Characters and ASCII Values
Computers do not store characters directly.
Instead, every character is converted into a number.
These numbers are part of a standard system called
ASCII
(American Standard Code for Information Interchange).
Examples:
So when you type a character, the computer stores it as a number internally.
---
2. Header File
This header gives access to cout (for printing messages) and cin (for taking user input).
---
#include <iostream>3. Declaring the char Variable
The program declares:
char ch;
char is a data type used to store a single character such as 'A', 'b', '3', or '$'.
It always stores exactly
1 byte
of memory.
---
4. Taking Character Input
The program asks the user:
cin >> ch;
Here's what happens:
If you type A, then ch holds 'A'.
If you type $, then ch holds '$'.
---
cout << "Enter a character: ";5. Converting char to int (Type Casting)
To print the ASCII number, the program uses:
(int)ch
This is called
type casting
.
Normally:
Type casting changes how the value is shown, not how it is stored.
---
6. Displaying the ASCII Value
The program prints:
If ch = 'A', the output will be:
If ch = 'b', output will be:
---
cout << "ASCII value of '" << ch << "' is: " << (int)ch << endl;7. return 0;
This ends the program and tells the computer everything worked fine.
---
Summary
Once you understand ASCII values, you'll be ready for more advanced topics like character functions, strings, and encryption basics.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for ASCII Value of a Character
This C++ program is part of the "Basic Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.