C++ Program to Find ASCII Value of a Character.

All Programs List Click

Source Code

#include <iostream>
using namespace std;

int main() {
   char c;
   cout << "Enter a character: ";
   cin >> c;
   cout << "ASCII Value of " << c << " is " << int(c);
   return 0;
}

Explanation:

  1. The character is read from the user using cin.
  2. The ASCII value of the character is obtained using the int() function, which converts the character to its ASCII equivalent.
  3. The ASCII value is then displayed using cout.

What is ASCII value?

ASCII (American Standard Code for Information Interchange) is a standard for representing characters as integers. It defines a mapping between the integers 0 through 127 and a set of characters, including the digits, uppercase and lowercase letters, punctuation marks, and other symbols.

Each character in the ASCII table is assigned a unique integer value, known as its ASCII value. For example, the ASCII value of the letter A is 65, the ASCII value of the letter a is 97, and the ASCII value of the digit 0 is 48.

The ASCII table is often used to represent characters in computer systems and communication networks, as it provides a standard way of encoding and decoding characters.

Explain “cout << “ASCII Value of ” << c << ” is ” << int(c);”

In the C++ programming language, cout is an object of the ostream class that represents the standard output stream (usually the console window). The << operator is used to send data to the standard output stream.

The line cout << "ASCII Value of " << c << " is " << int(c); sends the following four items to the standard output stream:

  1. The string "ASCII Value of "
  2. The value of the char variable c
  3. The string " is "
  4. The ASCII value of the char variable c, which is obtained by casting it to an int using the int() function

So, this line of code prints the ASCII value of the character c preceded by the string "ASCII Value of " and followed by the string " is ".

For example, if the value of c is 'A', this line of code would print the following to the standard output stream:

ASCII Value of A is 65