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.


What is a Modifier?

A modifier is used to change or adjust an existing type. There are different types of integer modifiers:

  1. short
  2. long
  3. long long

These modifiers can be added before an integer type to modify its default size and range.


Short Modifier

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.

Short Modifier with Signed Integer

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:

Short Modifier with Unsigned Integer

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.


Using Macros to Know the Minimum and Maximum Value

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:

  1. SHRT_MIN – gives the minimum value of short int.
  2. SHRT_MAX – gives the maximum value of short int.
  3. INT_MIN – gives the minimum value of int.
  4. INT_MAX – gives the maximum value of int.
  5. UINT_MAX – gives the maximum value of unsigned int.
  6. USHRT_MAX – gives the maximum value of  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

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.