Tokens in C++ Programming

Tokens

  • A token is a group of characters.
  • It is the smallest element of a C++ program which is meaningful to the compiler.
C++ uses the following types of Tokens:
1. Keywords
2. Identifiers
3. Data types
4. Operators

1. Keywords

  • Keywords are the reserved identifiers that have special meanings.
  • These reserved keywords cannot be used as identifiers in a program.
  • All keywords are written in lower case.
Following are the keywords used in C++:

asmdefaultfloatoperatorstatic_castunion
autodeleteforprivatestructunsigned
breakdofriendprotectedswitchusing
booldoublegotopublictemplatevirtual
casedynamicifregisterthisvoid
catchelseinlinereinterpret_castthrowvolatile
charenumintreturntruewchar_t
classexplicitlongshorttrywhile
constexternMutablesignedtypedef
const_castexportnamespacesizeoftypeid
continuefalsenewstatictypename

2. Identifiers

Identifier is a sequence of characters used to define various things like variables, constants, functions, classes, objects, structures, unions etc.

It follows the rules for the formation of an identifier:
  • An identifier consists of alphabets, digits or underscores.
  • It cannot start with a digit. It can start either with an alphabet or underscore.
  • Identifier should not be a reserved word.
  • C++ is case-sensitive. So, upper case and lower case letters are considered different identifiers from each other.
  • Blank spaces and special symbols are not allowed except underscore.

3. Data types

  • Data type is used for identifying the type of data and the memory locations are required for storing the type of data in the memory.
  • Whenever a variable is declared it becomes necessary to define data type.
Following are the C++ data types:

Data typeRangeBytes
char-127 to 1281
signed char-127 to 1281
unsigned char0 to 2551
int-32768 to 327672
signed int-32768 to 327672
unsigned int0 to 655352
short int-32768 to 327672
long int-2147483648 to 21474836474
signed short int-32768 to 327672
signed long int-2147483648 to 21474836474
unsigned short int0 to 655352
unsigned long int 0 to 42949672964
float3.4e-38 to 3.4e384
double1.7e-308 to 1.7e3088
long double3.4e-4932 to 1.1e493210

4. Operators

  • Operator is a symbol used to perform mathematical or logical manipulations.
  • It specifies the order of operations in expressions that contain more than one operator.
It includes all C operators and has several new operators, as follows:

OperatorDescription
Scope Resolution Operator (::)It is used to identify and disambiguate identifiers used in different scopes.
Unary Operator ( - )It returns the negative value of the variable to which it precedes.
Casting Operator ( () )It is used for type conversion.
Address of Operator ( & )It returns the address of a memory location.
sizeof() OperatorIt returns the size of a memory location.
Comma ( , ) OperatorIt allows grouping two statements where one is expected.
Indirection Operator or Value of Operator ( * )It defines pointer to a variable.
Ternary Operator ( ?: )It is a conditional expression.
NewCreates an instance of an object type.
DeleteDeletes property of an object.

Arithmetic Operators

OperatorDescription
+Addition
Subtraction
*Multiplication
/Division
%Modulus (Reminder)
++Increment
––Decrement

Comparison Operators

OperatorDescription
= =Equal To
!=Not Equal To
<Less Than
>Greater Than
<=Less Than or Equal To
>=Greater Than or Equal To

Assignment Operators

OperatorDescription
=Simple Assignment
+=Add and Assignment
-=Subtract and Assignment
*=Multiply and Assignment
/=Divide and Assignment
%=Modulus and Assignment

Logical Operators

OperatorDescription
&&Logical AND
||Logical OR
!Logical NOT

Bitwise Operators

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Left Shift
>>Right Shift

Scope Resolution Operator

  • Scope Resolution Operator is denoted by :: symbol.
  • It is used to define a function outside a class.
  • When Local variable and global variable having same name then local variable gets the priority.
  • C++ allows flexibility of accessing both the variables through a scope resolution operator.