Tokens
Tokens are smallest individual units in a program that has some meaning to the compiler. There
are different types of tokens in C++:-
1. Keywords or Reserved words
2. Identifiers
3. Constants
4. Operator
5. Separator
1. Keywords :
Keywords are pre-defined or reserved words in a C++ programming language.
Keywords are special words that are significant to the C++ compiler.
You cannot use keywords as variable names, function names, arrays name, class
name and structure name, because by doing so, we are trying to assign a new
meaning to the keyword which is not allowed.
int long float char while do for if
else double struct typedef sizeof switch case static
const break continue goto class new delete virtual
public private protected friend try catch return Inline
operator this
Note: All the keywords must be written in lower case.
2. Identifier:
Identifiers are used as the general terminology for naming of variables, functions,
arrays, class, and structure in a program.
C++ identifiers follow the following rules:-
i. It must begin with alphabets and underscore characters.
ii. It must not begin with digit but can contain digits within.
iii. Identifiers in C++ are case sensitive. (i.e. Uppercase and Lowercase letters are
distinct.)
iv. Identifier name cannot contain white spaces.
v. You cannot use keywords as identifiers.
Eg:
int age,_salary;
double sum; can be used as a variables name
void calAvgPrice();
void findTax(); can be used as a Function Name
pi
sigma can be used as symbolic constant
3. Constant:
Constants are also like normal variables in C++ programming language. But, the
only difference is, their values cannot be modified or changed by the program
once they are defined.
Constants refer to fixed values.
Constants are also called as literals.
Constants may belong to any of the data type.
There are two types of constant:-
I. Numeric constant
II. String or character constant
I. Numeric constant
Numeric constant is again two types:
a) Integer constant
b) Real or Floating point constant
(a) Integer constant
An integer constant is a signed or unsigned whole number. C++ language allows
three types of integer constant
(i) Decimal integer constant: It consist of sequence of digits and should not
begin with zero( 0 ).
Eg: 124, -179 etc
(ii) Octal integer constant: It consists of sequence of digits starting with
zero (0).
Eg: 0123, 0146 Ans: 83
(iii) Hexadecimal integer constant: It consists of sequence of digits
preceded by 0x.
Eg: Ox123 Ans: 291
Note: the default numeric system followed in C++ is decimal.
(b) Read or Floating point constant
Any signed or unsigned number with fractional part is called real or floating point
constant.
Eg: 0.254, 342.5, -10.5
II. String or character constant
Any string of characters enclosed in quotes is called string constant or character
constant.
There are two types of string constant
a) Single character string constant
Any letter or character enclosed in single quote is called single character
string constant.
Eg: ‘y’, ’+’ etc
b) String of character constant
Any string of characters consisting of letters, digits and symbols enclosed in
double quotes is called string of character constant.
Eg: “Sum=”
4. Operator:
• C++ supports a rich set of operators such as =, +, -, * etc that can be used according
to the need of the application.
• An operator is a symbol that tells the computer to perform certain mathematical or
logical manipulations.
• Operators are used in programs to manipulate data and variables.
• Operators are usually parts of mathematical or logical expression.
Types of Operators:
I. Arithmetic Operators
Arithmetic operators are used to performing addition, subtraction,
multiplication, division, and modulus. It acts as a mathematical operation.
Operator Description
+ ( Addition ) This operator is used to add the value of the operands.
– ( Subtraction ) This operator and is used to subtract two operands.
* ( Multiplication ) This operator is used to multiply the value of the
operands.
/ ( Division ) This operator is used to divide the left hand operator
with right hand operator.
% ( Modulus ) This operator is used to divide the left hand operator
with right hand operator and returns remainder.
II. Relational Operators or Comparison Operators
• Relational operator compares two numbers and returns a boolean value.
• This operator is used to define a relation or test between two operands.
• All the relational operators are binary operator.
Example: we may compare the age of two persons, compare the price of two items
etc.
Operator Description
< (Less This operator returns True, if the value of the left operand is
than) less than the value of the right operand, else it returns False.
> (Greater This operator returns True, if the value of the left operand is
than) greater than the value of the right operand, else it returns
False.
<= (Less This operator returns True, if the value of the left operand is
than or less than or equal to the value of the right operand, else it
equal to) returns False.
>= (Greater This operator returns True, if the value of the left operand is
than or greater than or equal to the value of the right operand, else it
equal to) returns False.
== (Equal This operator returns True, if two operands are equal, else it
to) returns False.
!= (Not This operator returns True, if two operands are not equal, else
equal to) it returns False.
III. Logical Operators
Logical operators are used to combining two or more conditions or relational
expression.
Operator Description
&& (Logical This operator returns True if both the operands are true,
AND)
otherwise, it returns False.
|| (Logical This operator returns True if either the operands are true,
OR)
otherwise it returns False.
! (Logical This operator returns True if an operand is False. It reverses
NOT)
the logical state of an operand.
IV. Assignment Operators
• Assignment operators are used to assign new value to a variable.
• The left side operand of the assignment operator is called variable and the
right side operand of the assignment operator is called value.
Syntax:
var = expression ;
Here, the data type of “var” must be compatible with the type of expression.
• It allows to create a chain of assignment
Eg:
int a,b,c;
a=b=c=100;
V. Increment and Decrement Operators (++,--)
Increment operator (++) is used to increase the value of variable by 1. It can be
written as prefix or postfix to a variable
Eg. a++ // postfix increment operator
++a; // prefix increment operator
Decrement operator (--) is used to decrease the value of variable by 1. It can be
written as prefix or postfix to a variable
a--; // postfix decrement operator
--a; // prefix decrement operator
Q: How does x++ differ from ++x?
Eg: int m=15;
int k=m++;
First it assign the value 15 to k and thereafter increases the value of m=16
Eg: int m=15;
int k=++m;
First it increases the value m to 16 and thereafter assign 16 to k.
VI. Conditional Operators (?:) or ternary operator (operator operating on 3 inputs).
Ternary operator is a conditional operator; it reduces the line of code while
performing the conditional or comparisons. It is the replacement of if-else or
nested if-else statements.
Syntax:
( Condition ) ? ( Statement1 ) : ( Statement2 );
Here Condition is the expression to be evaluated which will return the boolean
value. If the Condition result is True then Statement1 will be executed. If the
Condition result is false then Statement2 will be executed.
int a = 4;
int b = 9;
int min = ( a<b ) ? a : b;
cout<< min ;
Q: WAC++ program to accept two numbers from the user and print the maximum number by using
conditional operator.
Q: WAC++ program to accept one number from the user and check whether the given number is
odd number or even number by using conditional operator.
Q: WAC++ program to accept three numbers from the user and print the maximum number by
using conditional operator.
VII. Bitwise Operators
Bitwise operators are used for manipulation of data at value of bit level. These
operators are used for testing the bits, or shifting them to the right or left.
Operator Meaning
& Bitwise AND
| Bitwise OR
~ One’s complement or Bitwise inversion
^ Bitwise exclusive OR
<< Left shift bitwise operator
>> right shift bitwise operator
Eg: Left shift bitwise operator
Let a=13; its binary equivalent is 00001101
Consider
a<<2;
which shifts two bits to the left i.e two zeros are inserted at the right to shift two
bits at the left to move out.
00 insert 2 bits at right
00 001101
2 bits move out
Result: 00110100
Decimal=(52)10
Eg: Right shift bitwise operator
Let a=13; its binary equivalent is 00001101
Consider
a>>2;
which shifts two bits to the right i.e two zeros are inserted at the left to shift two
bits at the right to move out.
00 insert 2 bits at left
000011 01
2 bits move out
Result: 00000011
Decimal=3
Eg: Bitwise AND (&)
Let a=13; its binary equivalent is 00001101
b=6; its binary equivalent is 00000110
Q: What will be output of ( a & b) ?
00001101
& 00000110
00000100
Eg: Bitwise OR (|)
Let a=13; its binary equivalent is 00001101
b=6; its binary equivalent is 00000110
What will be output of ( a | b)?
00001101
| 00000110
00001111
Eg: Bitwise exclusive OR (bitwise XOR) (^)
Let a=13; its binary equivalent is 00001101
B=6; its binary equivalent is 00000110
What will be output of ( a ^ b)?
00001101
& 00000110
00001011
5. Separators:
Separators are symbols used to indicate where groups of code are divided and arranged.
Separator basically defines the shape and function of our code.
Name What it is used for?
Brackets[]: Opening and closing brackets are used as array element reference.
These indicate single and multidimensional subscripts.
Parentheses():
These special symbols are used to indicate function calls and
function parameters.
Braces{}:
These opening and ending curly braces marks the start and end of a
block of code containing more than one executable statement.
Comma ( , ):
It is used to separate consecutive identifiers in a variable declaration
also used inside “for loop” statements.
Semi colon ( ; ) :
It is used to separate statement.
Period ( . ):
It is used to separate a variable or method from a reference
variable.