Data Types:
A type of value that a variable can hold is called datatype. C++ supports
three types of datatypes, they are as follows :
1. User-defined datatypes
2. Built-in datatypes
3. Derived Datatypes
(i)User-defined datatypes : They are of 4 types
a) Structure
b) Union
c) Class
d) Enumeration
Structure: A Structure is a collection of data items of different data types
under a single name.
Syntax:
struct tagname
Datatype member1;
Datatype member2;
1
.
Datatypes member n;
};
Union : A union is a collection of data item of different data types. It is
similar to that of a structure.
Syntax:
union tagname
Datatype member1;
Datatype member2;
Datatypes member n;
} variable1, variable2….variable n;
Class: It is collection of objects.
It consists of Data members and Member Functions
Enumeration : It is a user defined data type which allows the user to
create its own data and define the values that the variables of this
datatype can take. The syntax is as follows:
enum datatype_name
Value1,Value2…value n
};
Built in Data Types
Integer Data type :
(i) byte : It has 8 bit integer value.
2
• It’s minimum value is -127
• It’s maximum value is +128
• Its default value is 0
• 1 byte= 8 bits
• It is used in arrays to reduce the space
• It is 4 times smaller than integer value.
Example : byte a=100;
(ii) short: It is a 16 bit integer value.
• It’s minimum value is -32767
• It’s maximum value is +32768
• It’s default value is 0
• It is two times smaller than integer value.
• It is used in arrays to reduce the space.
Example : short s=10000;
(iii) int: It is a 32 bit integer value
• It’s minimum value is -2^31
• It’s maximum value is 2^31-1
• It’s default value is 0
• It is used a default datatype for integer values
Example : int a=10000000;
(iv) long: It is a 64 bit integer value
• It’s minimum value is -2^63
• It’s maximum value is 2^63-1
• It’s default value is 0L
• This type is used when a wider range than int is used.
Example : long a=100000L
Floating Data types:
(i) Float: It is a 32 bit floating point value.
• The default value is 0.0f
• It is used to save memory in large arrays of floating-point numbers.
• It is never used for precise values such as : currency.
Example: float a=63.05f
(ii) double: It is a 64 bit floating point value
• Default value is 0.0d
• This datatype is used as default data type for decimal values.
• It should never be used for precise values such as currency.
Example : double a=63.05d
3
char: It is a 16 bit value
• Its minimum value is ‘\u000’ - > 0
• Its maximum values is ‘\ffff - > 35000
• It is used to store any character
Example : char name=’siddhartha’;
Boolean: It represents 1 bit of information.
• There are only two possible values i.e. True or False
• It is used for simple flags.
• Its default value is false.
Derived datatypes They are of 4 types
a) Array :It is a variable which is capable of holding fixed values in
contagious memory location. The general format of an array is
Syntax: Datatype arrayname[arraysize];
b) Function: A function is a block of code that performs a specific task
. They can be defined once and executed for desired number of
times by calling it from any part of the program.
Syntax: functiontype functionname(parameter list);
c) Pointer: A variable that stores address of another variable is called
pointer.
Syntax: datatype *ptr;