Character Set in C++
What is Character Set in C++?
“A character set is a collection of characters that can be used in C++ source code and data. This set contains letters, numerals, symbols, and control characters that the C++ programming language recognises and can use.”
In your C++ programmes, the character set supplies the building blocks for producing identifiers, keywords, literals, and other language features. The ASCII (American Standard Code for Information Interchange) character encoding, which is a widely used and standardised character encoding scheme, forms the foundation of the C++ character set. Characters in the ASCII character set comprise letters (both uppercase and lowercase), numerals, punctuation marks, and control characters, each of which is represented by a unique numeric code.
C++ has how many character sets?
In C++, there are two types of character representations: narrow characters (based on the ASCII character set) and wide characters (using 'wchar_t') enabling extended character support, particularly for languages with non-ASCII characters and internationalisation. However, the cornerstone of C++ programming is a single primary character set based on ASCII.
Here are some essential points of C++'s character set:
- ASCII Character Set: The ASCII (American Standard Code for Information Interchange) character set contains a variety of printable and non-printable characters. It specifies numeric codes for characters such as capital and lowercase letters, numerals, punctuation marks, and control characters.
- Escape Sequences: Escape sequences can be used in C++ to represent characters that are not immediately typable or viewable. The newline character, for example, is represented as 'n', the tab character as 't', and the backslash character as ''.
- Wide Characters: The wchar t data type and wide character string literals with the L prefix are also supported by C++. This is very handy when dealing with characters beyond the ASCII range, as in internationalisation and localization.
- Character Literals: Single quotes are used to represent characters such as 'A', '5', '%', and so on. These single characters are assigned integer ASCII values.
- Character Strings: Character strings are represented by double quotes, such as "Hello, world!" C-style strings are character arrays that are terminated by the null character '0'.
- Header Files: The cctype> header file contains functions for character classification, such as isalpha(), isdigit(), isalnum(), and so on. These functions are available in wide character variants in the cwctype> header file.
- Input and output: Character input and output can be handled by C++ streams (cin and cout). To work with characters and strings, you can use prepared input/output routines.
- Character Encodings: While ASCII is the most commonly used character encoding, current systems and programming frequently employ more complex character encodings such as UTF-8, UTF-16, and UTF-32 to accommodate a greater range of characters, including those from different languages and scripts.
![Character Set in C++](https://static.tutorialandexample.com/cpp/character-set-in-cpp1.png)
Categories of Character Set in C++
Characters in C++ can be classified into many groups based on their features and usage. Here are some examples of typical C++ character types:
- Uppercase and lowercase letters of the English alphabet (A-Z and a-z) are examples of alphabetic characters. They are frequently used as identifiers, keywords, and text manipulation tools.
- Numeric Digits: The numerals 0 through 9. They are employed in numeric literals, arithmetic operations, and other mathematical computations.
- Whitespace characters: Include spaces, tabs, newlines, and other similar characters used in source code formatting and style. The compiler frequently ignores them.
- Punctuation Characters: These characters include, among others, the period (.), comma (,), semicolon (;), and parentheses (() and ()). They are used to organise and distinguish code elements.
- Characters such as the dollar sign ($), underscore (_), and at symbol (@) are examples of special characters. They are frequently used as identifiers.
- Control characters, such as the newline character (n), tab character (t), and carriage return (r), are non-printable characters with control purposes.
- Escape Sequences: These are character combinations that begin with a backslash () and are used to represent special characters that are not directly typable, such as newline (n), tab (t), and backslash ().
Program
Here's a basic C++ program that displays character sets and contains explanations for each component:
#include <iostream>
using namespace std;
int main() {
// Alphabetic characters
char letter = 'A';
// Numeric digits
char digit = '7';
// Whitespace characters
char space = ' ';
// Punctuation characters
char punctuation = '!';
// Special characters
char special = '$';
// Control characters
char newline = '\n';
char tab = '\t';
// Escape sequences
char backslash = '\\';
// Wide character
wchar_t wideCharacter = L'Ω'; // Greek letter Omega
// Output
cout << "Alphabetic: " << letter << endl;
cout << "Numeric: " << digit << endl;
cout << "Whitespace: [" << space << "]" << endl;
cout << "Punctuation: " << punctuation << endl;
cout << "Special: " << special << endl;
cout << "Control - Newline: " << newline << endl;
cout << "Control - Tab: [" << tab << "]" << endl;
cout << "Escape - Backslash: " << backslash << endl;
wcout << "Wide Character: " << wideCharacter << endl;
return 0;
}
Output
![Character Set in C++](https://static.tutorialandexample.com/cpp/character-set-in-cpp2.png)
Explanation
- To allow input and output streams, we include the iostream> header.
- We declare variables for various types of characters and assign them specific characters.
- Each variable has a comment that explains which type of character it represents.
- The cout stream is used to display the characters and their category definitions.
- To construct a wide character literal, we use the wcout stream and prefix the character with L.
When you run the programme, the characters and their descriptions will be displayed. This programme shows several types of characters and how they might be utilised in C++ programming.