You are on page 1of 5

8.

DATA TYPES

Data Types of C++


The C++ language is a strictly typed language, in this each variable, expression and
conversions are strictly or properly checked by the compiler. This feature of C++
helps us to reduce the errors in programming.

Data Types: To solve the problems of real word the programmer should deal with
different types of data. The data should be stored in the computer memory and it
should be processed. To represent the different types of data and to store in
computer memory, C++ has different data types.

Data Type: It is a type of the data using which different types of data can be
represented.

The data type gives the information about the type of data stored in the variable
and a set of operations can be performed with the data.

The C++ data types can be classified as

1. Fundamental (Built- In) Data type: In C++ there are 5 basic data types.
1) Integer: The keyword int is used to declare integers. The whole numbers
can be declared and stored using integers.
The general form of integer declaration is
int variable list;
Ex: int a, b;
Here a and b are variables of type integers in which a whole number can
be stored. The C++ allocates 4 bytes (32 bits) of memory for integer
data.
There are different types of integers
Short int: to store small integers, allocates 2 bytes of memory.
Long int: to store large numbers, allocates 8 bytes of memory
Unsigned int: to store positive numbers, stores the sign of the number
also.
2) Character: The keyword char is used to declare characters. The single
characters can be declared and stored using char data type.

IPUC COMPUTER SCIENCE Page 1


8.DATA TYPES

The general form of character declaration is


char variable list;
char ch1, ch2;
Here ch1 and ch2 are character type of variables to store single
characters. The C++ allocates one byte (8 bit) of memory for char data.
The single characters are enclosed within the single quote.
3) Floating type: The keyword float is used to declare real numbers that is
the numbers with the fractional part. Small real numbers can be stored
using float.
The general form of float declaration is
float variable list;
float a, b;
Here a and b are variables used to store real numbers. C++ allocates 4
bytes (32 bits) for float data.
4) Double type: The keyword double is used to declare and store large
decimal numbers. The C++ allocates 8 bytes of space for double data.
The general form of double declaration is
double variable list;
double a, b;
Here a and b are the variables to store large real numbers.
5) Void type: This type of data type is normally used in function return
value or function arguments.
Void means empty or no values.
Ex: void output (void);
Here output () is a function which takes no arguments and returns no
value.
6) Boolean type: The keyword bool is used to declare Boolean type of
data. These stores either true or false that is 1 or 0. C++ allocates 1 bit of
space for this type of data. This is used in evaluating the expressions.
The general form of Boolean type is
bool variable list;
Ex: bool male;

IPUC COMPUTER SCIENCE Page 2


8.DATA TYPES

2. Derived Data type: The data types created using fundamental types are
called as derived data types. Derived data types of C++ are
1) Arrays
2) Functions
3) Pointers
4) References
3. User Defined Data type: The data type created using fundamental types
according to the requirements of the user is called as user defined data
types. They are
1) Classes
2) Structures
3) Unions
4) Enumerations
1. Variables: A meaningful name given to a data storage location in computer
memory.
Syntax: data type variable name list;
That is
Data type v1, v2,…..vn;
Depending on the type of the variable declared, the computer will assign
the number of bytes to that variable.
Ex: int num; Here num is a variable of type integer. The computer will
assign 4 bytes of memory for int variable.
The memory address of num variable can be accessed using &num, &
operator gives the address of the variable.
To assign the value to the variable, the syntax is
Variable name=value;
Ex: int a;
a=10;
char ch;
ch=’y’;
Expressions: An expression in C++ is a valid combination of operators and
operands.

IPUC COMPUTER SCIENCE Page 3


8.DATA TYPES

OR
An arrangement of identifiers, literals and operators that can be evaluated
to compute a value.
The types of expressions are
1. Arithmetic Expression: It is a valid combination of arithmetic operators
and operands.
There are three types
1) Integer Mode: These expressions are formed using integer values,
constants and arithmetic operators.
Ex: 20+7, 20/5 etc.
2) Real Mode: These expressions are formed using real values,
constants and arithmetic operators.
Ex: 9.8+7.6, 25.0/5.0 etc
3) Mixed Mode: These expressions are formed using real and integer
values, constants and arithmetic operators.
Ex: 25/5.0, 7%5+10 etc
2. Relational Expression: It is a valid combination of relational operators
and operands. These expressions evaluate to true or false value.
Ex: 3<7, 21>=10 etc
3. Logical Expression: It is a valid combination of relational operators,
logical operators and operands. Using logical operators, multiple
conditions can be checked.
Ex: if (marks>=35) && (marks<=100)
(a>b) || (b>c)

Evaluation of an Expression: In C++ the expressions are evaluated in


specific order. This is known as precedence of operators.
Ex: 5*6+8, in this * has the more precedence so multiplication operation
has performed first and then the addition.
So, 5*6=30+8=38
Declaring Constants: Constant is a variable whose value remains fixed
through out the execution of the program.
const data type identifier =3.145;
IPUC COMPUTER SCIENCE Page 4
8.DATA TYPES

const double PI=3.1415;


Order of applying operators:
1. Evaluate any expression enclosed in parentheses.
2. Evaluate the expression which involves multiplication and division
operation, if multiplication and division comes together then evaluate
from left to right.
3. Evaluate the expressions which involve addition and subtraction
operation. If addition and subtraction comes together evaluate from left
to right.
Ex: 10-25+5 here the expression 10-25 will be evaluated first and then
-15+5 will be evaluated.
Ex: 12*10/2 here the expression 12*10 will be evaluated first and then
22/2 will be evaluated.

IPUC COMPUTER SCIENCE Page 5