Tokens in C++ (Keywords, Identifiers, Constants, Strings, Operators, Special Symbols )
Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
Tokens and keywords are undoubtedly notable features of C++.
Tokens act as building blocks of a program. Just like a living cell is the smallest possible unit of life, tokens in C++ are referred to as the smallest individual units in a program. Keywords in C++ help the user in framing statements and commands in a language. Each keyword conveys a unique connotation to the compiler to perform a specific task. Just like the combination of words helps us in framing sentences, the combination of keywords helps us in framing statements to perform logical operations in a programming language. Simply combining keywords wouldn’t help to serve the purpose.
As we need to use proper grammar to form a meaningful sentence, we need to be well-acquainted with the syntax of C++ to instruct the compiler what to do. If these statements are not formed in a logical manner, they would sound gibberish and you would get a compilation error. Let’s discuss the concept of Tokens with Character set in C++ in detail.
1. C++ Character Set
Before we begin with C++ tokens, let us understand what Character set has to offer.
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.
To understand the concept in the better way, learn Data Types in C/C++
2. Tokens in C++
As discussed earlier, 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:
- Keywords
- Identifiers
- Constants
- Strings
- Special symbols
- Operators
3. 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.
Get the Samurai Technique to Learn Arrays in C and C++
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 | – | – | – | – | – |
4. 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.
Rules for C++ Identifiers
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++
Here is a table which illustrates the valid use of Identifiers:
Identifier Name | Valid or Invalid | Correction or alternative, if invalid | Elucidation if invalid |
5th_element | Invalid | element_5 | It violates Rule 1 as it begins with a digit |
_delete | Valid | – | – |
school.fee | Invalid | school_fee | It violates Rule 2 as it contains a special character ‘.’ |
register[5] | Invalid | Register[5] | It violates Rule 3 as it contains a keyword |
Student[10] | Valid | – | – |
employee name | Invalid | employee _name | It violates Rule 4 as it contains a blank space |
perimeter() | Valid | – | – |
5. 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:
- Integer constants – These constants store values of the int data type.
For instance:
const int data = 5;
- Floating constants – These constants store values of the float data type.
For instance:
const float e = 2.71;
Refer to Constants and Literals in C++ for a detailed description.
- Character constants – These constants store values of the character data type.
For instance:
const char answer = ‘y’;
- String constants – These constants are also of the character data type but differ in the declaration part.
For instance:
const char title[] = ‘‘DataFlair’’;
- Octal constants – The number system which consists of only 8 digits, from 0 to 7 is called the octal number system. The constant octal values can be declared as:
const int oct = 034;
It is the octal equivalent of the digit 28 in the decimal number system.
- Hexadecimal constants – The number system which consists of 16 digits, from 0 to 9 and alphabets ‘a’ to ‘f’ is called hexadecimal number system. The constant hexadecimal values can be declared as:
const int hex = 0x40;
It is the hexadecimal equivalent of the digit 64 in the decimal number system.
6. 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.
7. Special Symbols
Apart from letters and digits, there are some special characters in C++ which help you manipulate or perform data operations. Each special symbol has a specific meaning to the C++ compiler.
Here is a table which illustrates some of the special characters in C:
Special Character | Trivial Name | Function |
[ ] | Square brackets | The opening and closing brackets of an array symbolize single and multidimensional subscripts. |
() | Simple brackets | The opening and closing brackets represent function declaration and calls, used in print statements. |
{ } | Curly braces | The opening and closing curly brackets to denote the start and end of a particular fragment of code which may be functions, loops or conditional statements |
, | Comma | We use commas to separate more than one statements, like in the declaration of different variable names |
# | Hash / Pound / Preprocessor | The hash symbol represents a preprocessor directive used for denoting the use of a header file |
* | Asterisk | We use the asterisk symbol in various respects such as to declare pointers, used as an operand for multiplication |
~ | Tilde | We use the tilde symbol as a destructor to free memory |
. | Period / dot | The use the dot operator to access a member of a structure |
8. C++ Operators
Operators are tools or symbols which are used to perform a specific operation on data. Operations are performed on operands. Operators can be classified into three broad categories according to the number of operands used.
Unary: It involves the use of one a single operand. For instance, ’!’ is a unary operator which operates on a single variable, say ‘c’ as !c which denotes its negation or complement.
Binary: It involves the use of 2 operands. They are further classified as:
- Arithmetic
- Relational
- Logical
- Assignment
- Bitwise
- Conditional
Ternary: It involves the use of 3 operands. For instance, ?: is used to in place of if-else conditions.
Enhance Your Fundamental Skills with Operators in C/C++
9. Quiz on Tokens in C++
10. Summary
Now, you know why tokens in C++ are called as building blocks of a program. All the sub-parts of tokens are: keywords, identifiers, constants, strings, special symbols, and operators equally important and play a vital role in framing the programs in C++. Every part is discussed well, refer all the links above and get a detailed description of each part.
Feedback and suggestions are welcomed in the comment section!
Did we exceed your expectations?
If Yes, share your valuable feedback on Google
The name of the identifier must not exceed 31 characters. Can you please explain this, as I am able to use identifier above 31 characters.
So loving, I love everything about the write up.
wow i love this type of all information about c/c++