C unsigned short Data Type

In C, the unsigned short data type is used to store non-negative integer values within a limited range. Unlike the standard short type, which can store both positive and negative values, unsigned short can only store positive values, effectively doubling its upper limit.

The unsigned short data type is useful when dealing with small positive integer values while conserving memory.


1 Storage Size of unsigned short Data Type

The size of an unsigned short varies depending on the system architecture and compiler, but it typically follows this pattern:

TypeStorage Size
unsigned short2 bytes (16 bits)

On most systems, an unsigned short occupies 2 bytes, allowing it to store integer values ranging from 0 to 65,535.

2 Values Stored by unsigned short Data Type

The unsigned short data type stores only non-negative whole numbers (i.e., no decimals or fractions).

Example values that can be stored in unsigned short:

0, 100, 25000, 65535

3 Example: Declaring and Using unsigned short Variables

Let’s see a simple program demonstrating how to declare and use unsigned short variables in C.

main.c

</>
Copy
#include <stdio.h>

int main() {
    unsigned short num1 = 500;
    unsigned short num2 = 60000;
    unsigned short sum = num1 + num2;

    printf("Number 1: %u\n", num1);
    printf("Number 2: %u\n", num2);
    printf("Sum: %u\n", sum);

    return 0;
}

Explanation:

  1. We declare three unsigned short variables: num1, num2, and sum.
  2. We assign values 500 and 60000 to num1 and num2, respectively.
  3. The sum of these two numbers is stored in the sum variable.
  4. We print the values using printf(), using the %u format specifier, which is used for unsigned integers.

Output:

Number 1: 500
Number 2: 60000
Sum: 60500

4 Checking Storage Size of unsigned short Programmatically

We can determine the storage size of an unsigned short using the sizeof operator.

main.c

</>
Copy
#include <stdio.h>

int main() {
    printf("Size of unsigned short: %lu bytes\n", sizeof(unsigned short));
    return 0;
}

Output:

Size of unsigned short: 2 bytes

5 Minimum and Maximum Values of unsigned short

Since unsigned short only stores non-negative values, its range is:

Storage SizeMinimum ValueMaximum Value
2 bytes (16 bits)065,535

6 Getting Maximum and Minimum Values of unsigned short Programmatically

The maximum and minimum values of an unsigned short can be retrieved using limits.h.

main.c

</>
Copy
#include <stdio.h>
#include <limits.h>

int main() {
    printf("Minimum unsigned short value: %u\n", 0);
    printf("Maximum unsigned short value: %u\n", USHRT_MAX);
    return 0;
}

Output:

Minimum unsigned short value: 0
Maximum unsigned short value: 65535

Conclusion

In this tutorial, we explored the unsigned short data type in C, including:

  1. Its ability to store only positive whole numbers.
  2. Its typical storage size of 2 bytes (16 bits).
  3. How to get the storage size programmatically using sizeof().
  4. The minimum and maximum values it can store.
  5. How to retrieve these values using limits.h.