C++ Tokens
C++ Tokens
The group of characters that forms an Basic Building block is called as Token.
1. Identifiers
2. Keywords
3. Constants
4. Operators
Identifiers
Various data items with symbolic names in C++ is called as Identifiers. Following
data items are called as Identifier in C++ –
1. Names of functions
2. Names of arrays
3. Names of variables
4. Names of classes
1. C++ is case-sensitive so that Uppercase Letters and Lower Case letters are
different
2. The name of identifier cannot begin with a digit. However, Underscore can be
3. Only alphabetic characters, digits and underscore (_) are permitted in C++
4. Other special characters are not allowed for naming a variable / identifier
5. Keywords cannot be used as Identifier.
Program
#include <iostream.h>
void main()
int a;
int A;
cin>>a;
cin>>A;
In the above code, we declare two variables 'a' and 'A'. Both the letters are same but
they will behave as different identifiers. As we know that the identifiers are the case-
sensitive so both the identifiers will have different memory locations.
Note:- Identifiers cannot be used as the keywords. It may not conflict with the
keywords, but it is highly recommended that the keywords should not be used as the
identifier name. You should always use a consistent way to name the identifiers so
that your code will be more readable and maintainable.
Keywords
In C++, keywords are reserved identifiers which cannot be used as names for the
variables in a program.
Or
A list of 32 Keywords in C++ Language which are also available in C language are
given below.
A list of 30 Keywords in C++ Language which are not available in C language are
given below.
Constants refer to as fixed values, unlike variables whose value can be altered, constants -
as the name implies does not change, they remain constant. Constant must have to be
initialized at the time of creating it, and new values cannot be assigned later to it.
Integer Constants
As the name itself suggests, an integer constant is an integer with a fixed value, that
is, it cannot have fractional value like 10, -8, 2019.
For example,
We use a floating-point constant to represent all the real numbers on the number
line, which includes all fractional values.
For instance,
Each character is associated with its specific numerical value called the ASCII
(American Standard Code For Information Interchange) value.
o Arithmetic Operators
o Relational Operators
o Logical Operators
o Bitwise Operators
o Assignment Operator
o Unary operator
o Ternary or Conditional Operator
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
#include <iostream.h>
#include <conio.h>
using namespace std;
void main()
{
int x,y;
cout<<”Enter any 2 numbers”;
cin>>x>>y;
clrscr();
cout <<”Sum of 2 nos is ”<< x + y;
}
Output:-
Enter any 2 numbers
4
5
Sum of 2 nos is 9.
#include <iostream.h>
void main()
int x = 5;
int y = 3;
cout << x - y;
Output:- 2
Binary Multiplication operator (*):-
#include <iostream.h>
void main()
int x = 7;
int y = 4;
cout << x * y;
Output:- 28
#include <iostream.h>
void main()
int x = 15;
int y = 5;
cout << x / y;
Output: - 3
Binary Modulus Operator(%)
#include <iostream.h>
void main()
int x = 18;
int y = 6 ;
cout << x % y;
Output:- 3
#include <iostream.h>
void main()
int x = 5;
++x;
cout << x;
Output :- 6
Post-increment Operator :- This operator stores the value first then increment it
which is assigned to a variable.
#include <iostream.h>
void main()
int x = 5;
x++;
cout << x;
Output :- 6
#include <iostream.h>
void main()
int x = 7;
--x;
cout << x;
Output :- 6
Post-decrement Operator :- This operator stores the value first then decrement it
which is assigned to a variable.
#include <iostream.h>
void main()
int x = 5;
x--;
cout << x;
Output :- 4
Comparison Operators/Relational
Note: The return value of a comparison is either true (1) or false (0).
In the following example, we use the greater than operator (>) to find out if
5 is greater than 3:
Example
int x = 5;
int y = 3;
== Equal to x == y
!= Not equal x != y
#include <iostream.h>
void main()
int x = 5;
int y = 3;
return 0;
Output 0
Example2:-
#include <iostream.h>
void main()
int x = 5;
int y = 3;
return 0;
Output 1
Example 3:-
#include <iostream.h>
void main()
int x = 5;
int y = 3;
Output 1
Example 4:-
#include <iostream.h>
void main()
int x = 5;
int y = 3;
cout << (x < y); // returns 0 (false) because 5 is not less than 3
Output 0
Example 5:-
#include <iostream.h>
void main()
int x = 5;
int y = 3;
cout << (x >= y); // returns 1 (true) because five is greater than, or equal, to 3
Output 1
Example 6 :-
#include <iostream.h>
void main()
int x = 5;
int y = 3;
cout << (x <= y); // returns 0 (false) because 5 is neither less than or equal to 3
return 0;
Output 0
C++Logical Operators
Logical operators are used to determine the logic between variables or values:
&& Logical and Returns true if both statements x < 5 && x < 10
are true
! Logical not Reverse the result, returns false !(x < 5 && x < 10)
if the result is true
Ternary Operator:-
The conditional operator can often be used instead of the if else statement. Since it is the
only operator that requires three operands in c++, It is also called the ternary operator.
If Y is greater than 5 then 4 will be assigned to variable X or else the value 8 will be
assigned to X
C++ program to find greatest of 3 numbers by using conditional /ternary operator
# include <iostream.h>
void main()
{
int a, b, c, big ;
cin>>a>>b>>c;
Output :-
Enter three numbers:
4
1
8
The biggest number is 8;
Precedence of Operators in C++:-
The precedence of operator species that which operator will be evaluated first and
next. The associativity specifies the operators direction to be evaluated, it may be left
to right or right to left.