Chapter 6 (Data Types and Operator)
Chapter 6 (Data Types and Operator)
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)
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
In the expression a+b, + is the operator and a and b are the operands.
Classification of operators
(a) Arithmetic operators : Used for arithmetic operations such as addition(+), subtraction(-),
multiplication(*), division(/) and modulus (%).
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).
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
!(2>5) results in 1
• 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 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
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
(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.
Variable Initialisation:
The keyword const is used to create symbolic constants whose value can never be
during program execution.
A sample program
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello, Welcome to C++";
return 0;
}
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;
}