0
0
CConceptBeginner · 3 min read

What is short int in C: Explanation and Example

short int in C is a data type used to store smaller integer values, typically using less memory than a regular int. It usually occupies 2 bytes and can hold values from -32,768 to 32,767 depending on the system.
⚙️

How It Works

Think of short int as a smaller box to store whole numbers compared to a regular int. Because it uses fewer bytes (usually 2 bytes), it can hold fewer numbers but takes up less space in memory. This is like choosing a small container for small items to save room.

In C, the exact size of short int depends on the system, but it is guaranteed to be at least 2 bytes. It stores signed numbers by default, meaning it can hold both positive and negative values. The range is typically from -32,768 to 32,767, which covers many common small numbers.

💻

Example

This example shows how to declare a short int, assign a value, and print it.

c
#include <stdio.h>

int main() {
    short int smallNumber = 32000;
    printf("Value of short int: %hd\n", smallNumber);
    return 0;
}
Output
Value of short int: 32000
🎯

When to Use

Use short int when you need to save memory and know your numbers will stay within the smaller range it supports. For example, it is useful in embedded systems or devices with limited memory where storing many small numbers efficiently matters.

It is also handy when working with large arrays of numbers that don't require the full range of a regular int, helping reduce the program's memory footprint.

Key Points

  • short int usually uses 2 bytes of memory.
  • It stores signed integers with a typical range of -32,768 to 32,767.
  • It helps save memory when large numbers are not needed.
  • Use %hd format specifier to print short int values.

Key Takeaways

short int stores smaller integer values using less memory than int.
It typically occupies 2 bytes and holds values from -32,768 to 32,767.
Use it to save memory when numbers fit within its range.
Print short int values using the %hd format specifier.