C++ Tokens are the smallest individual units of a program.
C++ is the superset of C and so most constructs of C are legal in C++ with their meaning and usage unchanged. So tokens, expressions, and data types are similar to that of C.
Following are the C++ tokens : (most of c++ tokens are basically similar to the C tokens)
- Keywords
- Identifiers
- Constants
- Variables
- Operators
Keywords
Keywords are reserved words which have fixed meaning, and its meaning cannot be changed. The meaning and working of these keywords are already known to the compiler. C++ has more numbers of keyword than C, and those extra ones have special working capabilities.
There are 32 of these, and here they are
auto const double float int short struct unsigned break continue elseforlong signed switch void case default enumgoto register sizeof typedef volatile char do extern if return static unionwhile
There are another 30 reserved words that were not in C, are therefore new to C++, and here they are -
asm dynamic_cast namespace reinterpret_cast try bool explicit new static_cast typeid catch false operator template typename class friend privatethis using const_cast inline public throw virtual delete mutable protected true wchar_t
Identifiers
Identifiers are names given to different entries such as variables, structures, and functions. Also, identifier names should have to be unique because these entities are used in the execution of the program.
Identifier naming conventions
Only alphabetic characters, digits and underscores are permitted.
First letter must be an alphabet or underscore (_).
Identifiers are case sensitive.
Reserved keywords can not be used as an identifier's name.
Constants
Constants are like a variable, except that their value never changes during execution once defined.
There are two other different ways to define constants in C++. These are:
By using const keyword
By using #define preprocessor
Declaration of a constant :
const [data_type] [constant_name]=[value];
Variable
A variable is a meaningful name of data storage location in computer memory. When using a variable you refer to memory address of computer.
Syntax to declare a variable
[data_type] [variable_name];
Example
#include <iostream.h> int main() { int a,b;// a and b are integer variable cout<<" Enter first number :"; cin>>a; cout<<" Enter the second number:"; cin>>b; int sum; sum=a+b; cout<<" Sum is : "<<sum <<"\n"; return 0; }
Operator
C++ operator is a symbol that is used to perform mathematical or logical manipulations.
- Arithmetic Operators
- Increment and Decrement Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Misc Operators
Arithmetic Operators
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
Increment and Decrement Operators
Operator | Description |
---|---|
++ | Increment |
−− | Decrement |
Relational Operators
Operator | Description |
---|---|
== | Is equal to |
!= | Is not equal to |
> | Greater than |
< | Less |
>= | Greater than or equal to |
<= | Less than or equal to |
Logical Operators
Operator | Description |
---|---|
&& | And operator. Performs logical conjunction of two expressions.(if both expressions evaluate to True, result is True. If either expression evaluates to False, the result is False) |
|| | Or operator. Performs a logical disjunction on two expressions.(if either or both expressions evaluate to True, the result is True) |
! | Not operator. Performs logical negation on an expression. |
Bitwise Operators
Operator | Description |
---|---|
<< | Binary Left Shift Operator |
!= | Is not equal to |
>> | Binary Right Shift Operator |
~ | Binary One's Complement Operator |
& | Binary AND Operator |
^ | Binary XOR Operator |
| | Binary OR Operator |
Assignment Operators
Operator | Description |
---|---|
= | Assign |
+= | Increments, then assign |
-= | Decrements, then assign |
*= | Multiplies, then assign |
/= | Divides, then assign |
%= | Modulus, then assigns |
<<= | Left shift and assigns |
>>= | Right shift and assigns |
&= | Bitwise AND assigns |
^= | Bitwise exclusive OR and assigns |
|= | Bitwise inclusive OR and assigns |
Misc Operators
Operator | Description |
---|---|
, | Comma operator |
sizeOf() | Returns the size of a memory location. |
& | Returns the address of a memory location. |
* | Pointer to a variable. |
? : | Conditional Expression |