Chapter-8 Data Types: Introduction
Chapter-8 Data Types: Introduction
Chapter-8
DATA TYPES
Introduction
To understand any programming languages we need to first understand the elementary concepts
which form the building block of that program.
The basic building blocks include the variables, data types etc.
C++ provides a set of data types to handle the data that is used by the program.
Variable:
A variable is an object or element and it is allowed change during the execution of the program.
Variable represents the name of the memory location.
Declaration of a variable:
The syntax for declaring a variable is:
datatype variable_name;
The variable_name is an identifier. These variables are used to denote constants, arrays, function,
structures, classes and files.
The variables are named storage location whose values can be manipulated during program run.
Examples:
Some valid variables are:
reg_no, marks, name, student1, dob;
Some invalid variables are:
Double - keyword cannot be name of the variable.
Total marks - empty spaces are not allowed between variables names
2student - variable name should be begin with an alphabet
?result - variable should begin with alphabet or underscore only.
Initializing a variable:
The syntax to initialize a variable is:
data_type variable_name = value;
Example: Let b be a variable declared of the type int. then
int b = 100;
There are two values associated with a variable known as lvaue and rvalue. It means, for example,
let p be a variable declared of the type int. then
1|Page
Chapter 8- Data Types
int p = 100;
Memory Variable Values
Here, name of the variable is p
2000 p 100
values assigned to variable is 100 i.e. rvalue
memory address location is 2000 i.e. lvalue
Lvalue is the location value. It holds the memory address location at which the data value is stored.
Rvalue is the data value. It holds the value assigned to the variable by the programmer i.e. Rvalue
of p = 100.
C++ compiler allows us to declare a variable at run time. This is dynamic initialization. They can
be initialized anywhere in the program before they are used.
The access modifier ‘const’ prefixed along with the data type for a variable does not allow the
value to be changed at all throughout the program.
cont int a = 100;
The keyword const becomes an access modifier for the variables.
o A value can be assigned to lvalue only in an expression.
o Value can be assigned to a variable using the assignment operator ‘=’.
o The expression to the left of an assignment operator should always be an lvalue (memory
location) because that memory location should be available to store the rvalue.
o Constant identifiers can appear to the right of the assignment operator only since are not
lvalues.
Data Types:
Data Types can be defined as the set of values which can be stored in a variable along with the
operations that can be performed on those values.
The main aim of C++ program is to manipulate data.
C++ defines several types of data and each type has unique characteristics.
C++ data types can be classified as:
1. The fundamental data type(built-in data)
2. Derived Data type
3. User-defined data type
The simple or fundamental data types are the primary data types which are not composed of any
other data types.
The simple data types/fundamental data types include int, char, float, double and void.
2|Page
Chapter 8- Data Types
4|Page
Chapter 8- Data Types
6|Page