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:
- The character is read from the user using
cin
. - The ASCII value of the character is obtained using the
int()
function, which converts the character to its ASCII equivalent. - 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:
- The string
"ASCII Value of "
- The value of the
char
variablec
- The string
" is "
- The ASCII value of the
char
variablec
, which is obtained by casting it to anint
using theint()
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
Leave a Reply