Hey guys, what is up. I hope so you all are good and are doing quite well. In this particular post, we will be talking about what is the data type and type modifiers in C++.
WHAT IS DATA TYPE?
Data type helps us to know which type of data is it represents. It means to identify the type of data and operations associated with it.
There are 3 types of data types:
- Fundamental data types
- Derived data types
- User-defined derived data types.
FUNDAMENTAL DATA TYPE:
Those data types that are not composed of other data types are called fundamental data types. These are predefined in C++ and can be used directly.
1. INTEGER DATA TYPE (INT):
It is defined with integer int. Integers are whole numbers. These data types have no fractional part. To declare a variable, we write: <data type> <variable name>; Example – int number;
- The range of int datatype is from -32768 to +32767.
- The size of int is 2 bytes.
2. CHARACTER DATA TYPES (CHAR):
It is defined with the keyword char. It is used to store characters in C++. These data types are often called integer data types as they are internally stored as integers by their ASCII value. To declare a variable, we write: <data type> <variable name>; Example – char name;
- The range of char is from -128 to +127 or 0 to 255.
- The size of the char datatype is 1 byte.
3. FLOAT DATA TYPES (FLOAT):
A number having only a fractional part is a floating-point number. It is defined using the word float. To declare a variable, you write – <data type> <variable name>; Example – float radius;
- The range of float is from 3.4E-38 to 3.4E+38.
- The size of the float datatype is 4 bytes.
ADVANTAGES OF FLOAT DATA TYPES OVER INTEGER DATA TYPES ARE:
- They represent fractional value
- They can represent a much greater value but they are much slower than integers.
- Floating-point numbers have 7 digits of precision and also show 6 digits after the decimal.
4. DOUBLE DATA TYPE (DOUBLE):
They are also known as double-precision floating-point numbers. It is defined by the keyword double. Double is used similarly as that of the float data type. It is slower than float.
- It handles floating point numbers with 15 digits of precision.
- The range of double is from 5.0 × 10−345 to 1.7 × 10308.
- The size of double is 8 bytes.
5. VOID DATA TYPE:
This datatype specifies an empty set of values and is used for return-type functions that do not return/restore any value. No object of type void can be declare.
DERIVED DATA TYPES:
Data types that are built from fundamental data types are called derived data types and these are not predefined. These are:
USER-DEFINED DATA TYPES:
Derived data types that are defined by the user are called user-defined derived data types. These are:
We use a modifier to change the size and value of the base data type to fit various situations more precisely. Fundamental data types except void can be modified using modifiers.
The list of modifiers are as follows:
It is the default float, char, and integers. It stores both positive and negative numbers.
It is used with integer datatypes such as int and char. It doubles up the range of the positive side. Negative numbers are not acceptable.
It can be used for both int and double data types. It increases the range and size of the variable. For example, Long int takes 4 bytes while long double takes 10 bytes.
Short is used with int data types. It is only a clarifier. It is the same as an integer.
SIZE OF ALL DATA TYPES:
|TYPE||SIZE (IN BYTES)||MINIMAL RANGE|
|Char||1||-128 to 127|
|Unsigned char||1||0 to 255|
|Signed char||1||-128 to 127|
|Short||2||-32768 to 32767|
|Unsigned short||2||0 to 65,535|
|Signed short||2||-32768 to 32767|
|Int||2||-32768 to 32767|
|Unsigned int||2||0 to 65,535|
|Signed int||2||-32768 to 32767|
|Long||4||-2,147,483,648 to 2,147,483,647|
|Unsigned long||4||0 to 4,294,967,295|
|Signed long||4||-2,147,483,648 to 2,147,483,647|
In the upcoming post, you will be getting information related to the latest technology. So stay tuned for upcoming posts. If you haven’t read about programming language then go and read that article and share your valuable comments.