CHAPTER 6
Data Types and Operators
Data types: Used to identify nature and type of data stored in a variable.
Fundamental data types:
Memory
Data type Type of value Eg:
(bytes)
void null or empty data 0
char character values 1 ‘A’, ‘\n’
Integral
int integer values 4 84, -4 datatypes
Numerical
float real values 4 datatypes
Floating
Real values(more point
double 8 datatypes
precision than float)
Float data has a precision of 7 digits and double data has a precision of 15 digits
Type modifiers
• Used to change the size, range or precision of a data type
• Type modifiers in C++ are long, short, signed and unsigned
Variables: Names given to memory locations.
Eg: int Num=18;
1001 i. Variable name : The name of the variable. (Num)
18 ii. Memory address(L-value):The memory address.(1001)
Num iii. Content(R-value) :The value stored in the variable.(18)
Operators: Symbols that indicate an operation. Eg: +, <, *, &&
In the expression a+b, + is the operator and a and b are the operands.
Classification of operators
1. Based on number of operands:
Category No. of operands Example
Unary 1 Unary +, unary -, ++,--
Binary 2 +, &&,<
Ternary 3 ?:
2. Based on nature of operation
(a) Arithmetic operators : Used for arithmetic operations such as addition(+), subtraction(-),
multiplication(*), division(/) and modulus (%).
x y x+y x-y x*y x/y x%y
7 3 10 4 21 2 1(remainder)
(b) Relational operators:
Used for comparing numeric data, resulting in either true (1) or false (0). The relational
operators are <(less than), >(greater than), <=(less than or equal to), >=(greater than or
equal to, ==(equal to), !=(not equal to).
x y x<y x>y x<=y x>=y x==y x!=y
7 3 0 1 0 1 0 1
(c) Logical operators:
Used to combine logical values, resulting in true or false. The logical operators are &&(AND),
|| (OR) and ! (NOT)
E1 E2 E1&&E2 E1||E2
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1
Eg: (2>5) && (4<6) results in 0
(2>5) || (4<6) results in 1
!(2>5) results in 1
Input / Output operators:
• The get from (extraction) operator (>>) is used for input. Eg: cin>>a;
• The put to (insertion) operator (<<) is used for output. Eg: cout<<a;
Assignment operator (=)
• Used to store a value in a variable. Eg: a=5;
Difference between = and == operators.
= ==
Assignment operator Relational operator
Assigns value to a variable Compares values
Arithmetic assignment operators (C++ short hands)
The operators which combine arithmetic and assignment operators. They are +=, -=, *=, /=, %=
eg: a=a+5 can be given as a+=5 a=a/10 can be given as a/=10
Increment (++) and Decrement (--) operators
++ is used to increment the value in a variable by 1.
-- is used to decrement the value in a variable by 1.
a++ (post increment form) and ++a (pre increment form) are same as a=a+1 or a+=1
a-- (post decrement form) and --a (pre decrement form) are same as a=a-1 or a-=1
(A post-form denotes use, then change method and a pre-form denotes change, then use
method)
n=--m;
sizeof operator
It is a unary compile time operator that returns the amount of memory space(in bytes)
allocated for the operand. Eg: sizeof (int) --> gives the value 4 , sizeof(3.2) --> gives the value 8
Precedence of operators
It is the order in which the operations are performed in an expression. The order of execution is ( ),
++, --, !, Unary +, Unary -, sizeof, *, /,%, +, - etc
Expressions: Combination of operators and operands.
(a) Arithmetic expressions: contains arithmetic operators. Eg: m + n * y
(i) Integer expressions: contains integer operands, Result is integer.
(ii) Floating point(Real) expressions: contains real operands. Result is float
value.
(iii) Constant expressions: contains constant values. (eg: 5+7/3)
(b) Relational expressions: contains relational operators. Result is either true or false. Eg:
x >y
(c) Logical expressions :contains logical operators. Result is either true or false. Eg: a<b &&
c>d
Type conversion
• Conversion of the data type of an operand to another.
Two types of conversion:
1. Implicit (Type promotion): This is done by the compiler and the conversion is from
lower data_type to higher.
Eg: 5/2.0 => 2.5 (Here, int datatype of 5 is converted to float by compiler. Thus the result of
the float expression is also float)
2. Explicit (Type casting): This is done by the programmer explicitly and conversion can
be to any data_type.
Eg: 5/(int)2.0 => 2 (Here, programmer uses type casting (int) to convert the float data type
of 2.0 to int. Thus the result of this integer expression is also an integer.)
Statements: Smallest executable unit of a program. C++ statement ends with semicolon
(;)
1. Declaration statement: Specifies the type of data that will be stored in a variable.
Syntax: datatype var_name;
eg: float avg;
Variable Initialisation:
(a) Supplying the initial value to a variable at the time of declaration.
eg: int n =5 ; or int n(5);
(b) Supplying the initial value to a variable during execution (dynamic initialisation).
eg: float sum=a+b;
const – The Access Modifier
The keyword const is used to create symbolic constants whose value can never be
during program execution.
Eg: const float pi = 3.14;
2. Assignment statements: It stores a value to a variable. Eg: a=15;
3. Input statements: Specifies an input operation. Eg: cin>>score;
4. Output statement: Specifies output operation. Eg: cout<<score;
The multiple use of input/output operators in a single statement is called cascading of
I/O operators.
Eg: cin>>x>>y>>z;
cout<<”Sum=”<<S;
Structure of a C++ Program
#include <headerfile> ---------------- > line 1
using namespace identifier; -------- > line 2
int main() ----> line 3
{
statements;
return 0;
}
Line 1: Preprocessor directive :
Instructs the compiler to perform an action before actual compilation. Starts with the
symbol #.
eg: #include<iostream> --instruction to link the header file iostream.
Line 2: namespace statement:
Tells the compiler to use namespace std in this program. std is the standard namespace in
which a lot of objects, including cin and cout are defined.
Line 3: Function header:
main() is the essential function for every C++ program. A C++ program execution starts and
ends within the main() function.
A sample program
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello, Welcome to C++";
return 0;
}
Guidelines for coding
• Use suitable naming convention for identifiers
• Use clear and simple expressions
• Use comments wherever needed
• Give proper indentation
Comments
Lines in code that are added to describe the program. They provide for internal
documentation.
There are two ways to write comments in C++:
• Single line comment: The characters // (two slashes) is used to write single line comments.
• Multiline comments: Anything written within /* and */ is treated as comment
Write a C++ program to input two numbers and find their sum.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum;
cout<<"Enter two numbers: ";
cin>>num1>>num2;
sum=num1+num2;
cout<<"Sum of the entered numbers = "<<sum;
return 0;
}