C++ Tokens
A token is the smallest unit that a C++ compiler can handle. It can be categorized into different types namely:-
Keywords (or reserved words):- Keywords (or reserved words):- These are words used for specific purposes or for predetermined tasks. They must be written in small letters or lowercase letters. Some of the keywords used in C++ are int, float, char, double, long, void, for, while, do, if, else…
Keywords cannot be used for:-
- Declaring Variable Name
- Declaring Class Name
- Declaring Function Name
- Declaring Object Name
- Constants are those elements whose value cannot be changed while the program is running.
Identifiers:- This is a name used to represent a variable constant, table name, function name, class, object, etc. There are some rules while defining an identifier:-
- i) The first character of the identifier should be an alphabet or underscore. The rest may be letters, numbers, and underscores.
- ii) Special characters, other than the underscore, may not be included in identifiers.
- iii) Keywords cannot be used as identifiers; however, keywords written in upper case can be used as identifiers.
- iv) Lower case and upper-case letters are distinct.
- v) Maximum length of an identifier can be 31 characters. However, r the length varies from one version of the compiler to another version.
Valid examples:- sum, a1, _1, _a2, mean1, a_bc, x123y…
Invalid examples:- 1a, a-b, float
Constants:- A value that remains constant throughout the program execution is known as constant. An identifier may be stated as a constant as shown below:-
- i) # define N 20
- ii) # define PI 3.14
- iii) const int M=35; or const m=35;
Note:- Where the constant identifier is an integer, the int keyword is optional. As a default, the identifier is considered as an integer. A single value can be reported as a constant at a time with the const keyword.
Variables:- An identifier whose value may be changed is known as a variable. A variable can be declared as shown below:-
int a;
float x;
char ch;
Here a is a variable of type integer, x is a variable of type float, whereas ch is a
variable of type char. The values can be assigned with a, x & ch as shown below.
a=10; x=7.5; ch=’d’; a=a+5; x=x-4;
Literals:- The constant values used in C++ are known as literals, there are four types of literals namely:-
- i) Integer constant:- A value written without fraction part is known as an integer constant. Example: 25, -674, 0 etc.
- ii) Floating constant:- A value written with fraction unit is floating value. Such a value can be written with or without the exponent form. Example: 2.34, -9.2154, 1.21E10
- iii) Character constant:- A single character written within single quotation marks is known as a character constant. Example: ‘g’, ‘9’, ‘$’, etc
- iv) String constant:- This is an array of characters surrounded by quotation marks. Example: “Shubham”, “03-Aug-2009”. A double quotation mark is a delimiter that determines the length of a string.
