/    /  CPP – C++Tokens

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:-

  1. Declaring Variable Name
  2. Declaring Class Name
  3. Declaring Function Name
  4. Declaring Object Name
  5. 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:-

  1. i) The first character of the identifier should be an alphabet or underscore. The rest may be letters, numbers, and underscores.
  2. ii) Special characters, other than the underscore, may not be included in identifiers.
  3. iii) Keywords cannot be used as identifiers; however, keywords written in upper case can be used as identifiers.
  4. iv) Lower case and upper-case letters are distinct.
  5. 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:-

  1. i) # define N 20
  2. ii) # define PI 3.14
  3. 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:-

  1. i) Integer constant:- A value written without fraction part is known as an integer constant. Example: 25, -674, 0 etc.
  2. 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
  3. iii) Character constant:- A single character written within single quotation marks is known as a character constant. Example: ‘g’, ‘9’, ‘$’, etc
  4. 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.