As we know, data types are essential to declare a variable in our program, but isn’t the efficiency of our program important? Don’t you think so? The answer is “Yes, it is“; hence, in C++, an introduction to data type modifiers is crucial. Let’s understand the Modifiers in C++ to get the answer “what are the modifiers?“
What are the Modifiers?
Modifiers are nothing but an extension to the data types, making using the built-in or primitive data types more efficient along with our program by modifying the meaning of the primary type so that the requirements of any case or situation can be satisfied.
We use these data type modifiers as a prefix in primitive data types, which improves the primitive data types and enhances their usage.
In C++, we have about four types of data type modifiers, that are:
- Signed
- Unsigned
- Long
- Short
We can use these modifiers as a prefix in front of the int, float, double, and char, i.e., the primitive data types we learned about in our primitive data types blog.
Types Of Data type Modifiers
Signed
We use this modifier as a prefix for only int and char data types. It stores only the Positive, Negative, and the Zero when used for integer data types. The range of the maximum and minimum values a signed int variable can store is -2147483648 to 2147483647, and a signed char is this -127 to 127 or 0 to 255.
Unsigned
An unsigned modifier is used only for the int and char data types. But unlike the signed modifiers, it stores only a positive or zero value. The maximum and minimum values range that a variable declared as unsigned int is 0 to 4294967295, and an unsigned char is 0 to 255.
Short
As the name suggests, this modifier helps modify the minimum values an int data type can hold. Short int and short are equivalent to each other, hence can be used synonymously. The range of the values is −32,767 to +32,767.
Long
This modifier helps modify the maximum values a data type can hold. Even long int and long are equivalent to each other, hence can be used synonymously. The range of the values long int can hold is -2,147,483,648 to 2,147,483,647.
The following table shows various data types with modifiers and their size in bytes
Modifier | Size | Ranges |
---|---|---|
short int/ short | 2 Bytes | -32768 to 32767 |
signed short | 2 Bytes | -32768 to 32767 |
unsigned short | 2 Bytes | 0 to 65,535 |
int | 4 Bytes | -2147483648 to 2147483647 |
signed int | 4 Bytes | -2147483648 to 2147483647 |
unsigned int | 4 Bytes | 0 to 4294967295 |
long int/ long | 8 Bytes | -2,147,483,648 to 2,147,483,647 |
signed long | 8 Bytes | -2,147,483,648 to 2,147,483,647 |
unsigned long | 8 Bytes | 0 to 4,294,967,295 |
long long int | 8 Bytes | -(2^63) to (2^63)-1 |
unsigned long long int | 8 Bytes | 0 to 18,446,744,073,709,551,615 |
float | 4 Bytes | 1.2E-38 to 3.4E+38 (6 decimal places) |
double | 8 Bytes | 2.3E-308 to 1.7E+308 (15 decimal places) |
long double | 16 Bytes | 3.4E-4932 to 1.1E+4932 (19 decimal places) |
char | 1 Byte | -127 to 127 or 0 to 255 |
signed char | 1 Byte | -127 to 127 |
unsigned char | 1 Byte | 0 to 255 |
CONCLUSION
So here we got to know all about the extensive knowledge of the core fundamentals in C++, i.e., data types modifiers. Hence you’ve done it well. Practice it once on your compiler/onlinegdb to understand and grasp it well, as coding is all about practical, and theory alone doesn’t render the result and enhance the learning.
Similarly, you must also know that GeekonPeak has just begun, and it is looking for some loyal supporters so we, the new-gen geeky friends, can grow and create more extensive and easy-to-grasp courses for you (for free). You can follow us on Facebook, Instagram & Twitter as well. Also, don’t miss our newsletter subscription. We provide exclusive blogs, tips, tricks, and advice to program efficiently and market smartly. We also promote some of our partners with excellent experiences to share with you. Until next time 🙂