C++ Character set is basically a set of valid characters that convey a specific connotation to the compiler. We use characters to represent letters, digits, special symbols, white spaces, and other characters.The C++ character set consists of 3 main elements. They are:
- Letters: These are alphabets ranging from A-Z and a-z (both uppercase and lowercase characters convey different meanings)
- Digits: All the digits from 0 – 9 are valid in C++.
- Special symbols: There are a variety of special symbols available in C++ like mathematical, logical and relational operators like +,-, *, /, \, ^, %, !, @, #, ^, &, (, ), [, ], ; and many more.
Combinations of characters is refereed as Token. Tokens in C++ are the smallest individual unit of a program. The following tokens are available in C++ which are similar to that seen in C with the addition of certain exclusive keywords, strings, and operators:
C++ Keywords: Keywords in C++ refer to the pre-existing, reserved words, each holding its own position and power and has a specific function associated with it. It is important to note that we cannot use C++ keywords for assigning variable names as it would suggest a totally different meaning entirely and would be incorrect. Here is a list of keywords available in C++ according to the latest standards:
alignas, alignof, asm, auto, bool, break, case, catch, char, char16_t, char32_t, class, const, constexpr, const_cast, continue, decltype default, delete, double, do, dynamic_cast, else, enum, explicit, export, extern, FALSE, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, noexcept, nullptr, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_assert, static_cast, struct, switch, template, this, thread_local, throw, TRUE, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while
C++ Identifiers: C++ allows the programmer to assign names of his own choice to variables, arrays, functions, structures, classes, and various other data structures called identifiers. The programmer may use the mixture of different types of character sets available in C++ to name an identifier. There are certain rules to be followed by the user while naming identifiers, otherwise, you would get a compilation error. These rules are:
- First character: The first character of the identifier in C++ should positively begin with either an alphabet or an underscore. It means that it strictly cannot begin with a number.
- No special characters: C++ does not encourage the use of special characters while naming an identifier. It is evident that we cannot use special characters like the exclamatory mark or the “@” symbol.
- No keywords: Using keywords as identifiers in C++ is strictly forbidden, as they are reserved words that hold a special meaning to the C++ compiler. If used purposely, you would get a compilation error.
- No white spaces: Leaving a gap between identifiers is discouraged. White spaces incorporate blank spaces, newline, carriage return, and horizontal tab.
- Word limit: The use of an arbitrarily long sequence of identifier names is restrained. The name of the identifier must not exceed 31 characters, otherwise, it would be insignificant.
- Case sensitive: In C++, uppercase and lowercase characters connote different meanings. Before we move ahead, you should know how to declare and define variables in C/C++
C++ Constants: Before we begin our discussion on constants in C++, it is important to note that we can use the terms “constants” and “literals” interchangeably. As the name itself suggests, constants are referred to as fixed values that cannot change their value during the entire program run as soon as we define them.
Syntax: const data_type variable_name = value;
Types of Constants in C++
The different types of constants are:
- const int data = 5; Integer constant
- const float e = 2.71; Floating constant
- const char answer = ‘y’; Character constant
- const char title[] = ‘‘DataFlair’’; String constant
- const int oct = 034; Octal constant
- const int hex = 0x40; Hexadecimal constant
It is the hexadecimal equivalent of the digit 64 in the decimal number system.
C++ Strings: Just like characters, strings in C++ are used to store letters and digits. Strings can be referred to as an array of characters as well as an individual data type.
It is enclosed within double quotes, unlike characters which are stored within single quotes. The termination of a string in C++ is represented by the null character, that is, ‘\0’. The size of a string is the number of individual characters it has.
In C++, a string can be declared in the following ways:
- char name[30] = ‘’Hello!”; // The compiler reserves 30 bytes of memory for the string.
- char name[] = “Hello!”; // The compiler reserves the required amount of memory for the string.
- char name[30] = { ‘H’ , ’e’ , ’l’ , ’l’ , ’o’};; // This is how a string is represented as a set of characters.
- string name = “Hello” // The compiler reserves 32 bytes of memory.
No comments:
Post a Comment