Integer Data Type with Short Modifier
In this tutorial, we will learn about using and the importance of a short modifier on integer data types.
In this tutorial, we will learn about using and the importance of a short modifier on integer data types.
A modifier is used to change or adjust an existing type. There are different types of integer modifiers:
These modifiers can be added before an integer type to modify its default size and range.
By adding the keyword short
before an integer type, its default size can be reduced. This is useful when a programmer wants to store small integer values and tells the compiler to do the same. It helps save space.
The syntax for using the short modifier with a signed integer is:
short int variable_name;
or
int short variable_name;
Consider the following program:
#include <stdio.h>
int main()
{
short int var = -40;
printf("The integer value stored in variable var is %hd and ", var);
printf("the size of short integer in my machine is %d bytes", sizeof(var));
}
Output:
The output shows that the size of the short integer on my machine is 2 bytes, which is smaller than the size of an integer (usually 4 bytes, but it can vary depending on the machine).
Notice that %hd
is used as a format specifier to display the short integer value, and the sizeof()
operator is used to print the size of the variable var
in bytes. sizeof()
may look like a function, but it is actually an operator.
The range of a short int can be calculated using the following formula:
where n represents the number of bits.
Therefore, short int type of 2 bytes (16 bits) has the following range:
To use the short modifier with an unsigned integer, you can follow the syntax:
short unsigned int variable_name;
Note that you can change the order of “short,” “unsigned,” and “int” in any way you want.
Consider the following program:
#include <stdio.h>
int main()
{
short unsigned int var = 40;
printf("The integer value stored in variable var is %hu and ", var);
printf("the size of unsigned short integer in my machine is %d bytes", sizeof(var));
}
Output:
The program and its output are self-explanatory. Notice that the size of a short unsigned int and an unsigned int is the same, which is 2 bytes.
The range of a short unsigned int type with 2 bytes (16 bits) is:
Important Point to Remember:
The size of a short int or short unsigned int may not be less than the size of an int on some machines. However, it is certain that the size of an int can never be less than the size of a short int or a short unsigned int.
If you need to find the minimum and maximum values of a specific type, you can use macros specifically designed for this purpose. These macros are available in the <limits.h>
header file.
Here are 6 macros available in the <limits.h>
header file to determine the minimum and maximum values of short int
, int
, unsigned int
, and unsigned short int
:
It is worth noting that the minimum value of unsigned integer types is always zero.
The following program prints the minimum and maximum values of integer types, including those with short modifiers:
#include <stdio.h>
#include <limits.h>
int main()
{
printf("The minimum and maximum values of short int type are %hd and %hd respectively.\n", SHRT_MIN, SHRT_MAX);
printf("The minimum and maximum values of int type are %d and %d respectively.\n", INT_MIN, INT_MAX);
printf("The maximum values of unsigned int and unsigned short int types are %u and %hu respectively.", UINT_MAX, USHRT_MAX);
return 0;
}
Output:
The respective format specifiers are used to print the minimum and maximum values. You can refer to this article to learn about all the format specifiers for primitive data types.
Leave a comment