Data type modifiers in C are keywords used to modify the size, range, or sign of primitive data types such as int, char, and double. They help customize data types to store different ranges of values efficiently.
- Modifiers can increase or decrease the range of values a data type can store.
- They can specify whether a data type stores only positive values (unsigned) or both positive and negative values (signed).
#include <stdio.h>
int main() {
// Using unsigned int and trying
// to store negative value
unsigned int ui = -100;
printf("ui: %u", ui);
return 0;
}
Output
ui: 4294967196
Explanation: An unsigned int cannot represent negative values, so when -100 is assigned to ui, it wraps around to the maximum value of unsigned int plus 1 and subtracts 100, resulting in 4294967196. The %u format specifier ensures the value is printed as an unsigned integer.
Type of data type modifiers
Data type modifiers are used to change the size, range, or sign of primitive data types according to the program's requirements. The four main data type modifiers in C are short, long, signed, and unsigned.
Short Modifier
The short modifier is used with integer data types to reduce their storage size and range. It typically decreases the size of an int to 2 bytes (16 bits).
- Uses less memory compared to a standard int, making it useful when storing small integer values.
- A short int typically stores values from -32,768 to 32,767.
#include <stdio.h>
int main() {
int a;
// Modify the int using short
short int b;
printf("Size of int: %d\n", sizeof(a));
printf("Size of short int: %hd", sizeof(b));
return 0;
}
Output
Size of int: 4 Size of short int: 2
Explanation: We initialized a normal integer 'a' and short integer 'b'. After that, we have printed the size of both the variables and we can clearly see in the output that variable declared with data modifier with 'short' became 2 bytes
Note: The sizes of data type considered in this article are according to 32-bit compiler.
Long Modifier
The long modifier is used to increase the storage size and range of certain data types, primarily int. It allows the variable to store larger values than a regular int.
- A long int can store a wider range of integer values compared to a standard int.
- It generally uses more memory, allowing larger numbers to be represented.
#include <stdio.h>
int main(){
// Defining variables with long
int num;
long int long_num;
long long int long_long_num;
double dub_num;
long double long_dub_num;
printf("num: %d", sizeof(num));
printf("\nlong_num: %d", sizeof(long_num));
printf("\nlong_long_num: %d",
sizeof(long_long_num));
printf("\ndub_num: %d", sizeof(dub_num));
printf("\nlong_dub_num: %d",
sizeof(long_dub_num));
return 0;
}
Output
num: 4 long_num: 8 long_long_num: 8 dub_num: 8 long_dub_num: 16
Explanation: We have declared the variables of integer and double type with and without using long modifier and then printed the size of each variable. In the output, we can see that the size of data type with long modifier becomes double.
Unsigned Modifier
The unsigned modifier is used with integer data types to store only non-negative values. By removing the sign bit, it increases the maximum positive value that can be stored.
- An unsigned int stores values from 0 to a higher positive limit compared to a signed int.
- It can be combined with other modifiers such as short and long to create unsigned short and unsigned long.
#include <stdio.h>
int main() {
// Maximum value for unsigned int (32-bit system)
unsigned int n1 = 4294967296;
// Larger value stored in unsigned long int
unsigned long int n2 = 4294967296;
printf("Unsigned int (n1): %u\n", n1);
printf("Unsigned long int (n2): %lu\n", n2);
return 0;
}
Output
Unsigned int (n1): 0 Unsigned long int (n2): 4294967296
Signed Modifier
The signed modifier indicates that a data type can store both positive and negative values. It is the default modifier for int and char if no modifier is explicitly specified.
- The available range is divided between positive and negative numbers.
- Variables of type int and char are signed by default on most systems.
#include <stdio.h>
int main(){
int n1 = 248;
signed int n2 = 23124;
printf("%d", sizeof(n1));
printf("\n%u", sizeof(n2));
return 0;
}
Output
4 4
Size and Range of Data Types with Modifiers
The below table lists the size and the range of data type (in 64-bit compiler) that is changed with the help of modifiers:
| Data Type | Modifiers | Size (bytes) | Range |
|---|---|---|---|
| char | signed | 1 | -128 to 127 |
| unsigned (default) | 1 | 0 to 255 | |
| short int | signed (default) | 2 | -32,768 to 32,767 |
| unsigned | 2 | 0 to 65,535 | |
| int | signed (default) | 4 | -2,147,483,648 to 2,147,483,647 |
| unsigned | 4 | 0 to 4,294,967,295 | |
| long int | signed (default) | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| unsigned | 8 | 0 to 18,446,744,073,709,551,615 | |
| long long int | signed (default) | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| unsigned | 8 | 0 to 18,446,744,073,709,551,615 | |
| double | None | 8 | ~1.7E-308 to 1.7E+308 |
| long double | None | 16 | Higher precision, range varies depending on implementation |