In the previous article, we have discussed about C++ Program to Find GCD. Let us learn how to Print ASCII Value of a Character in C++ Program.
Program to Print ASCII Value of a Character in C++
In this article, we will write a program of how we can print the ASCII value of a character in c++ language.
First of all, let see what an ASCII value is.ASCII is the acronym for the American Standard Code for Information Interchange. It is a code for representing 128 English characters as numbers, with each letter assigned a number from 0 to 127. For example, the ASCII code for uppercase M is 77.
Method 1-Using typecasting
This is the approach for getting the ASCII value of a character. When we typecasting string into int then we get the ASCII value of that character. Let’s write the code for this.
#include <iostream> using namespace std; int main() { char c = 'a'; cout << "The ASCII value of " << c << " is " << int(c); return 0; }
Output
The ASCII value of a is 97
By this method, we can get the ASCII value of any character.