In this example, you will learn how to find the ASCII value of a character in C++.
ASCII (American Standard Code for Information Interchange) is the most common character encoding format for text data on computers and the internet. In standard ASCII-encoded data, there are unique values for 128 alphabetic, numeric, or special additional characters and control codes.
The ASCII value of A to Z is the ASCII characters from 33 to 126.
Ask the user to enter any character. Read the character into a character type variable ch. Get the ASCII value of the character by using the function int(expression)
.
The int(expression)
function returns the numeric integer equivalent to a given expression.int(ch)
will return the numerical value of the entered character.
Step 1: Call the header file iostream
.
Step 2: Use the namespace std.
Step 3: Open the integer type main function; int main().
Step 4: Declare character type variable ch;
Step 5: Ask the user to enter a character.
Step 6: Read the character into the variable ch
Step 7: Get the numerical value of the entered character by using the function int (ch)
Step 8: Print the value
Step 9: Exit.
#include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
cout << "ASCII Value of " << ch << " is " << int(ch);
return 0;
}
Run 1 Enter a character: 4 ASCII Value of 4 is 52 Run 2 Enter a character: t ASCII Value of t is 116