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++.
(Updated on 24/9/2021)
KEY POINTS
WHAT IS DATA TYPE?
Data type helps us to know which type of data is it represents. It determines to identify the type of operations and data and 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 (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 two (2) bytes.
2. CHARACTER (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 one (1) byte.
3. FLOAT (FLOAT):
A number having only a fractional part is a floating-point number. It is defined using the word float. To declare a variable using float, we 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 four (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 (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 datatype is from 5.0 × 10−345 to 1.7 × 10308.
- The size of double is eight (8) bytes.
5. VOID:
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:
- Array
- Function
- Pointers
- References
- Constants
USER-DEFINED:
Derived data types are defined by the user or programmers according to their needs and are known as user-defined derived data types. These are:
- Classes
- Structure
- Union
- Enumeration
DATATYPE MODIFIERS:
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:
- Signed
- Unsigned
- Long
- Short
SIGNED:
It is the default float, char, and integers. It can store all real numbers that means both positive and negative numbers.
UNSIGNED:
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.
LONG:
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:
Short is used with int data types. It is only a clarifier. It is similar to 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 Tokens in C++. 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.