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++:
asm | default | float | operator | static_cast | union |
auto | delete | for | private | struct | unsigned |
break | do | friend | protected | switch | using |
bool | double | goto | public | template | virtual |
case | dynamic | if | register | this | void |
catch | else | inline | reinterpret_cast | throw | volatile |
char | enum | int | return | true | wchar_t |
class | explicit | long | short | try | while |
const | extern | Mutable | signed | typedef | |
const_cast | export | namespace | sizeof | typeid | |
continue | false | new | static | typename | |
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 type | Range | Bytes |
---|
char | -127 to 128 | 1 |
signed char | -127 to 128 | 1 |
unsigned char | 0 to 255 | 1 |
int | -32768 to 32767 | 2 |
signed int | -32768 to 32767 | 2 |
unsigned int | 0 to 65535 | 2 |
short int | -32768 to 32767 | 2 |
long int | -2147483648 to 2147483647 | 4 |
signed short int | -32768 to 32767 | 2 |
signed long int | -2147483648 to 2147483647 | 4 |
unsigned short int | 0 to 65535 | 2 |
unsigned long int | 0 to 4294967296 | 4 |
float | 3.4e-38 to 3.4e38 | 4 |
double | 1.7e-308 to 1.7e308 | 8 |
long double | 3.4e-4932 to 1.1e4932 | 10 |
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:
Operator | Description |
---|
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() Operator | It returns the size of a memory location. |
Comma ( , ) Operator | It 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. |
New | Creates an instance of an object type. |
Delete | Deletes property of an object. |
Arithmetic Operators
Operator | Description |
---|
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (Reminder) |
++ | Increment |
–– | Decrement |
Comparison Operators
Operator | Description |
---|
= = | Equal To |
!= | Not Equal To |
< | Less Than |
> | Greater Than |
<= | Less Than or Equal To |
>= | Greater Than or Equal To |
Assignment Operators
Operator | Description |
---|
= | Simple Assignment |
+= | Add and Assignment |
-= | Subtract and Assignment |
*= | Multiply and Assignment |
/= | Divide and Assignment |
%= | Modulus and Assignment |
Logical Operators
Operator | Description |
---|
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
Bitwise Operators
Operator | Description |
---|
& | 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.