Table of Contents
ToggleIn this article we will into the ASCII table in C++, exploring its various character sets—from control characters to printable characters, including letters, digits, and special symbols—and demonstrates how to interact with these characters through C++ programming.
What is ASCII Table In C++?
The ASCII table is a character-encoding scheme originally based on the English alphabet. ASCII stands for American Standard Code for Information Interchange. It encodes 128 specified characters into seven-bit integers as shown by the ASCII standard. These characters include letters (both uppercase and lowercase), digits, punctuation marks, and non-printable control characters.
In total, there are 256 ASCII characters which can be divided into three types:
ASCII Control Characters
ASCII control characters are the first 32 characters in the ASCII table, ranging from 0 to 31. These characters are not printable and are used to control peripherals such as printers or to control the display of text. For example, ASCII 13 represents a carriage return.
ASCII Printable Characters
The ASCII printable characters start from 32 to 126 in the ASCII table. These characters include uppercase and lowercase letters, digits, punctuation marks, and a few miscellaneous symbols. For example, ASCII 65 represents the uppercase letter ‘A’, and ASCII 97 represents the lowercase letter ‘a’.
ASCII Printable Characters can be categorized into several types, including special characters, numbers, and alphabets.
1. Special Characters
Special characters in ASCII include punctuation marks, mathematical symbols, and other miscellaneous symbols that are neither letters nor numbers. These characters play crucial roles in formatting, operations, and syntax within programming and text representation.
Range:
- Starting from ASCII code 32 (
space
) to 47 (/
), which includes symbols likespace
,!
,"
,#
,$
,%
,&
,'
,(
,)
,*
,+
,,
,-
,.
, and/
. - From 58 (
:
) to 64 (@
), including:
,;
,<
,=
,>
,?
, and@
. - From 91 (
[
) to 96 (`), encompassing[
,\\
,]
,^
,_
, and`
. - From 123 (
{
) to 126 (~
), covering{
,|
,}
, and~
.
2. Numbers
The ASCII table includes a range for decimal digits, making it straightforward to represent numbers in texts.
Range:
- From 48 (
0
) to 57 (9
). This range represents the ten decimal digits from 0 to 9.
3. Alphabets
ASCII characters include both uppercase and lowercase alphabets, allowing for a wide range of textual data representation.
Uppercase Alphabets Range:
- From 65 (
A
) to 90 (Z
). This range includes all 26 letters of the English alphabet in uppercase form.
Lowercase Alphabets Range:
- From 97 (
a
) to 122 (z
).This range includes all 26 letters of the English alphabet in lowercase form.
ASCII 8: Extended ASCII Characters
The Extended ASCII is an 8-bit character set that includes the standard ASCII characters (0-127) and an additional 128 characters (128-255), bringing the total to 256. These additional characters include various symbols, diacritics, and graphical symbols. However, it’s important to note that the extended ASCII can vary depending on the system or standard being used (such as ISO-8859-1 or Windows-1252).
Some of the Extended ASCii characters:
ASCII Value | Character | ASCII Value | Character | ASCII Value | Character | ASCII Value | Character |
---|---|---|---|---|---|---|---|
128 | Ç | 144 | É | 160 | á | 176 | ░ |
129 | ü | 145 | æ | 161 | í | 177 | ▒ |
130 | é | 146 | Æ | 162 | ó | 178 | ▓ |
131 | â | 147 | ô | 163 | ú | 179 | │ |
132 | ä | 148 | ö | 164 | ñ | 180 | ┤ |
133 | à | 149 | ò | 165 | Ñ | 181 | ╡ |
134 | å | 150 | û | 166 | ª | 182 | ╢ |
135 | ç | 151 | ù | 167 | º | 183 | ╖ |
136 | ê | 152 | ÿ | 168 | ¿ | 184 | ╕ |
137 | ë | 153 | Ö | 169 | ⌐ | 185 | ╣ |
138 | è | 154 | Ü | 170 | ¬ | 186 | ║ |
139 | ï | 155 | ¢ | 171 | ½ | 187 | ╗ |
140 | î | 156 | £ | 172 | ¼ | 188 | ╝ |
141 | ì | 157 | ¥ | 173 | ¡ | 189 | ╜ |
142 | Ä | 158 | ₧ | 174 | « | 190 | ╛ |
143 | Å | 159 | ƒ | 175 | » | 191 | ┐ |
Programs To Print ASCII Value in C++
Let’s look some of the C++ programs to print ascii value.
C++ Program to Print ASCII Value of 0 to 9
#include<iostream>
using namespace std;
int main() {
for(char c = '0'; c <= '9'; c++) {
cout << "ASCII value of " << c << " is " << int(c) << endl;
}
return 0;
}
Output
ASCII value of 0 is 48
ASCII value of 1 is 49
ASCII value of 2 is 50
ASCII value of 3 is 51
ASCII value of 4 is 52
ASCII value of 5 is 53
ASCII value of 6 is 54
ASCII value of 7 is 55
ASCII value of 8 is 56
ASCII value of 9 is 57
Explanation: This program iterates from the character ‘0’ to ‘9’, converting each character to its ASCII integer value using type casting (int(c)) and prints it.
C++ Program To Print ASCII Value of a Character
#include<iostream>
using namespace std;
int main() {
char c;
cout << "Enter a character: ";
cin >> c;
cout << "ASCII value of " << c << " is " << int(c) << endl;
return 0;
}
Output:
ASCII value of A is 65
Explanation: This program prompts the user to enter a character, then prints the ASCII value of the entered character by casting the character to an integer.
C++ Program to Print ASCII Value of A to Z using Type Conversion
#include<iostream>
using namespace std;
int main() {
for(char c = 'A'; c <= 'Z'; c++) {
cout << "ASCII value of " << c << " is " << static_cast<int>(c) << endl;
}
return 0;
}
ASCII value of A is 65
ASCII value of B is 66
ASCII value of C is 67
...
ASCII value of X is 88
ASCII value of Y is 89
ASCII value of Z is 90
Explanation: This program loops from ‘A’ to ‘Z’ and uses static_cast<int>()
for type conversion to print the ASCII value of each uppercase letter.
Why is ASCII Table In C++ Important?
Understanding and using the ASCII table in C++ is crucial for various programming tasks, such as data encoding/decoding, character manipulation, and implementing communication protocols. It allows developers to work with characters in a standardized way, ensuring compatibility and predictability in how text is processed and displayed across different systems and platforms.
Check out
More C++ tutorials here
I hope You liked the post 👍. For more such posts, 📫 subscribe to our newsletter. Try out our free resume checker service where our Industry Experts will help you by providing resume score based on the key criteria that recruiters and hiring managers are looking for.
FAQ
What is ASCII 7 and ASCII 8?
ASCII 7, also known as 7-bit ASCII, is a character encoding standard that uses 7 bits to represent 128 unique characters, including letters, digits, and control characters. ASCII 8, or Extended ASCII, expands on this by using an extra bit (making it 8 bits in total) to represent 256 characters, allowing for additional symbols, special characters, and international characters not found in the standard ASCII set.
How to write ASCII code?
Writing ASCII code involves using numeric values to represent characters in text. For example, in programming, you can use the numeric ASCII value with a specific syntax (like \x41
for ‘A’ in C++ or Python) to insert characters into strings or to perform character operations. This approach allows programmers to directly manipulate text at its fundamental, numeric level, enabling precise control over data processing and display tasks.