Q16. Define datatype. Also, explain different datatypes in C++.
Answer :
Data Type
A data type indicates the type of data value stored in’a variable. The data type of a variable is specified at the time of its declaration and is attached till the end of execution of the program.
Data Types in C++
C++ supports four basic data types (i.e., int, float, char, double) which can be categorized as follows,
- Integral data type
- Floating point data type.
1.Integral Data Types
Integral data types are used to store whole numbers and characters. It can be further classified as,
(i) Integer data type
(ii) Character data type.
(i) Integer Data Type
This data type is used to store the whole numbers. The integers do not contain the decimal points. They receive input in the binary form for storage. The size of the integer can be either 16 or 32 bits and the range of integer values is -32768 to +32767.
(ii) Character Data Type
This data type indicates that the value stored in the associated variable is a character. Each character has an equivalent ASCII value (example ‘A’ has an ASCII value of 65).
The size and range of values for character data types are shown in table
2. Floating-point Data Types
Floating-point data types are used to store real numbers . (i.e., decimal point numbers). The keyword ‘float’ is used to declare a variable holding a floating-point number. The floating point numbers with 6 digit of precision occupies 4 bytes for storage. For greater precisions, the ‘double’ data type can be used and (i.e., 14 digits of precision) for further precision, long double can be used.
The size and range of values for floating-point data types are given in table below.
(ii) Character Data Type
This data type indicates that the value stored in the associated variable is a character. Each character has an equivalent ASCII value (example ‘A’ has an ASCII value of 65).
The size and range of values for character data types are shown in table
Character Data Type
|
|||
Type
|
Size (Byte)
|
Range
|
Format Specifier
|
char (or) signed char
|
1
|
-128 to 127
|
%c
|
unsigned char
|
1
|
0 to 255
|
%c
|
2. Floating-point Data Types
Floating-point data types are used to store real numbers . (i.e., decimal point numbers). The keyword ‘float’ is used to declare a variable holding a floating-point number. The floating point numbers with 6 digit of precision occupies 4 bytes for storage. For greater precisions, the ‘double’ data type can be used and (i.e., 14 digits of precision) for further precision, long double can be used.
The size and range of values for floating-point data types are given in table below.
Type Size Range Format
|
|||
Type
|
Size (Byte)
|
Range
|
Format Specifier
|
float
|
4
|
-3.4E -38 to 3.4E +38
|
|
double
|
8
|
-1.7E -308 to 1.7E +308
|
|
long double
|
10
|
-1.7E -4932 to 1.7E +4932
|
|