Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The short (or short int) data type in C is a primitive integer type that allocates less or equal memory compared to a standard int. By default, it represents signed whole numbers and is guaranteed by the C Standard to be at least 16 bits (2 bytes) in width, strictly adhering to the architectural constraint sizeof(short) <= sizeof(int).

Type Variants and Syntax

The short keyword can be combined with signedness modifiers. If no modifier is provided, the type defaults to signed. The int keyword is optional and usually omitted in idiomatic C.
short a = 10;               // Implicitly signed, equivalent to 'signed short int'
signed short b = -500;      // Explicitly signed
unsigned short c = 65000;   // Unsigned, strictly non-negative

Size and Value Ranges

While the exact byte size is architecture-dependent, a short is almost universally implemented as a 16-bit two’s complement integer. The exact limits for the target compiler are defined via macros in the <limits.h> header.
TypeMinimum SizeStandard Range (16-bit)<limits.h> Macros
signed short16 bits-32,768 to 32,767SHRT_MIN / SHRT_MAX
unsigned short16 bits0 to 65,5350 / USHRT_MAX

Format Specifiers

When performing I/O operations with standard library functions like printf or scanf, the h (half) length sub-specifier must be prepended to the base integer format specifiers.
#include <stdio.h>

int main(void) {
    short s_val = -1234;
    unsigned short u_val = 56789;

    // %hd for signed decimal
    printf("Signed: %hd\n", s_val);
    
    // %hu for unsigned decimal
    printf("Unsigned: %hu\n", u_val);
    
    // %hx or %ho for unsigned hexadecimal or octal
    printf("Hexadecimal: %hx\n", u_val);

    return 0;
}

Integer Promotion

A critical mechanical behavior of short in C is integer promotion. When a short is evaluated in an arithmetic or bitwise expression, the compiler implicitly promotes its value to an int (or unsigned int if an int cannot represent the entire range of the original type) before executing the operation.
#include <stddef.h>

void promotion_example(void) {
    short x = 32000;
    short y = 1000;

    // x and y are promoted to 'int' before the addition occurs.
    // The expression (x + y) yields an 'int', not a 'short'.
    size_t expr_size = sizeof(x + y); // Evaluates to sizeof(int), typically 4 bytes

    // Assigning an out-of-range 'int' back to a signed 'short' results in Implementation-Defined Behavior.
    // Unlike unsigned types (which guarantee modulo arithmetic/truncation), signed conversion is compiler-dependent.
    short z = x + y; 
}
Master C with Deep Grasping Methodology!Learn More