C++ programming language supports many data types that define a set of values and a set of operations on those values. The computer manipulates various types of data. The basic data types are as follows:
|int||Stores numeric values|
|float||Stores real values|
|double||Stores large real values|
|char||Stores character values|
C++ also modifies these data types and the derived data types are known as data type modifiers. The derived types are used to add more meaning to the base data types that is they can store a large amount of values. For example if we want to store 4 bytes in an int data type we can’t do this without modifying the data type this is very simple, we can use the type modifier of int data type.
Only int, double and char data types can be modified in C++ programming language.
The type modifiers for an int data type are as follows:
- Short int (it takes two bytes in memory)
- unsigned int (it stores only positive integer values)
- long int (it stores larger values)
- Unsigned long int (it is used to store larger positive values)
|Data type||Size in Bytes||Ranges|
|int||2||-32768 to 32767|
|short||2||-32768 to 32767|
|Unsigned int||2||0 to 65535|
|long||4||-2147483648 to 2147483647|
|Unsigned long||4||0 to 4294967295|
The floating point data is a numeric value with decimal point or fraction. It is also called real type value.
The type modifiers for float type data are as follows:
- Float (it is use to store real values)
- Double (it is used to store large real values)
- Long double (it is used to store very large real values)
|Data type||Size in Bytes||Description|
|float||4||3.4*10^-38 to 3.4*10^38|
|double||8||1.7*10^-308 to 1.7*10^308|
|Long double||10||1.7 *10^-4932 to 1.7*10^4932|
Character data type is used to store character value. It takes 1byte in memory.
Only signed and unsigned modifiers can be applied to char data type.
Each type of numeric variable can store a particular minimum and maximum value. An error occurs if the value assigned to the variable is less than the minimum value or more than a maximum value.
An overflow occurs when the value assigned to a variable is more than the maximum possible value. An underflow occurs when the value assigned to a variable is less than possible minimum value.
For example, an integer variable can store values from -32768 to 32768. If the assigned value is more than 32768, it is known as integer overflow. But if the value is less than -32768, it is called integer underflow.
The overflow and under flow is not detected by the compiler. It may generate wrong results. It is the responsibility of the user to ensure that the values assigned to the variables are within the allowed range.
Consider the following program:
Note: endl is used to generate a new line in C++ like \n.
Type qualifiers refer to the number of bytes used for storing the integer on a particular computer. You have to refer to your computer manual to find out the sizes on your computer.