One of the basic data types of programming is characters, one, two or four bytes of data elements. Characters are the part of strings and strings are important in every step of programming and they should be used carefully and displayed carefully they should be as global as possible if you are developing global applications that use different languages. Understanding how to use character literals in your C++ code is an important skill to master. Choosing the right professional C++ App Tool will also help you be able to visualize character literals and strings by using special syntax highlighting which can also help prevent making simple but time-consuming mistakes.
During the development of modern applications, sometimes we need to know the character Literals that we use to define the character form of a character. A character literal is used to define the representation of a single character’s value within the source code of a computer program. Now let’s see how we can use character literals in C++.
Table of Contents
Why do we need character literals in Modern C++?
In the C/C++ programming language, ASCII codes are used as char arrays to store texts in ASCII mode. In early C and C++ versions, we were using char arrays as a string only. More international applications require greater compatibilities for character display support in other human languages, locale supports, support for emojis, etc. The 8 bits of ASCII character formats were not enough to hold these wider character sets, so we moved to 16 bits of character forms for the strings and then on to 32bits and more. Thus, there are different string types to accommodate the different sizes of characters. This started with
UnicodeString, etc. Unicode strings are the most broadly compatible strings that we can use in C++.
Those different character bits require a new character declaration with a letter. This is called character literals.
What are character literals in Modern C++?
A character literal is a letter that represents sequence of characters or escape sequences enclosed in single quotation mark symbols, i.e ‘x’. A character literal may be prefixed with a character literal letter – u, for example u’x’. Here, as we can see in the table below,
u is a character literal for the ‘x’ character value to indicate it is character of type
Character literals are used to define character format of a strings. A character literal is used to define the representation of a single character’s value within the source code of a computer program.
What are types of character literals are there in Modern C++?
Basically there are 5 character literals, in the first one we use nothing, others are u8, u, U, L. Here is a table that we prepared for you that shows all character literals
|Character Literal||Syntax||Char Types||Standard||Example||C++ Examples|
|char||char c = ‘c’;|
char c = ‘\n’;
char c = ‘\13’;
|u8||char8_t||since C++20||u8’c’||char8_t c = u8’c’;|
char8_t c = u8’\n’;
char8_t c = u8’\13′;
|u||u||char16_t||since C++11||u||char16_t c = u’c’;|
char16_t c = u’\n’;
char16_t c= u’\13′;
|U||U||char32_t||since C++11||U||char32_t c = U’c’;|
char32_t c = U’\n’;
char32_t c = U’\13′;
|L||L||wchar_t||L||wchar_t c = L’c’;|
wchar_t c = L’\n’;
wchar_t c = L’\13′;
Note that, here
char8_t is a new type in C++20 and requires the
/std:c++latest compiler option.
Is there an example of character literals in Modern C++?
If you want to see an example that shows all character literals in usage, here it is,
int _tmain(int argc, _TCHAR* argv)
char c1 ='A';
// char8_t c2 = u8'A'; // (C++20)
char16_t c3 = u'A';
char32_t c4 = U'A';
wchar_t c5 = L'A';
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.