Printing Character’s ASCII Value in C++ Program
The ASCII code is a system for representing characters in digital communications. It assigns a unique code, made up of seven or eight bits, to each character, including a range of characters such as uppercase and lowercase letters, digits, punctuation marks, and various other symbols.
Introduction to ASCII Codes
ASCII is the acronym for the American Standard Code for Information Interchange, which is a character encoding standard used to represent characters as numbers. It is a collection of 255 symbols in the character set. Each character in the ASCII character set is assigned one unique number between 0 and 127, which is known as its ASCII code. This allows computers to store and manipulate characters as numerical values.
For instance, the ASCII code for the uppercase letter “A” is 65, while the code for the lowercase letter “a” is 97. The use of ASCII codes ensures that each character is represented consistently across different systems and applications. The ASCII codes for the characters are fixed, which means that every computer that uses the ASCII standard will interpret the same number as the same character. This allows for the exchange of text data between different computer systems.
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
Methods to Print ASCII Value in C++ of a Character
Using Type Conversion To Print ASCII Value in C++
Below is an example to print the ASCII value of a character in C++ using type conversion
#include <iostream>
using namespace std;
int main() { char c;
cout << "Enter a character: "; cin >> c;
int asciiValue = (int)c;
cout << "The ASCII value of character " << c << " is " << asciiValue << endl;
return 0;}
Output 1
Enter a character: 8
The ASCII value of character 8 is 56
Output 2
Enter a character: E
The ASCII value of character E is 69
In this code, we use type conversion to convert the character entered by the user to its corresponding integer value, which is the ASCII code for that character.
Explore free C++ courses
Using While Loop To Print ASCII Value in C++
Following is an example of how to print the ASCII value of a character entered by the user in C++ using a while loop
#include <iostream>
using namespace std;
int main() { char c;
cout << "Enter a character: "; cin >> c;
while (c != '#') { cout << "The ASCII value of " << c << " is: " << int(c) << endl; cout << "Enter another character or enter # to quit: "; cin >> c; }
return 0;}
Output
Enter a character: P
The ASCII value of P is: 80
Enter another character or enter # to quit: p
The ASCII value of p is: 112
Enter another character or enter # to quit:
In this example, the user is prompted to enter a character, and the ASCII value of that character is printed. This program will continue to run until the user enters the character ‘#’, which is used as a signal to stop the loop. The int() function is used to perform a type conversion and obtain the ASCII code of the character as an integer.
Endnotes
ASCII is still widely used today, but it has been largely replaced by other character encoding standards such as Unicode, which supports a much larger set of characters and symbols. Despite its limitations, ASCII remains an important part of computer science and is frequently used in low-level programming and communication protocols. Hope this article was helpful for you to understand how to print ASCII codes of characters in C++. Explore our C++ articles to find out more about the language and consolidate your knowledge of the fundamentals.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio