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.

short (formally short int) is a fundamental, statically-typed integer data type in C++ designed to allocate a smaller or equal amount of memory compared to the standard int type. The C++ standard mandates that a short must be at least 16 bits in width.

Memory and Range Specifications

Assuming the standard 16-bit implementation (2 bytes), the numerical limits are dictated by the sign representation (typically Two’s Complement):
  • signed short (Default): Ranges from -32,768 to 32,767.
  • unsigned short: Ranges from 0 to 65,535.
You can query the exact limits on your target architecture using the <climits> header via the SHRT_MIN, SHRT_MAX, and USHRT_MAX macros, or by using std::numeric_limits<short>::max() from the <limits> header.

Syntax and Declaration

The int keyword is optional when declaring a short. By default, short is signed.
// Signed declarations (semantically identical)
short val1 = 10;
short int val2 = -500;
signed short val3 = 32000;

// Unsigned declarations (semantically identical)
unsigned short val4 = 65000;
unsigned short int val5 = 42;

Architectural Guarantees

The C++ standard defines relative size constraints rather than absolute byte sizes for fundamental types. The strict data model guarantee for short is:
1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

Integer Promotion

When performing arithmetic or bitwise operations on short variables, C++ applies integer promotion. The compiler implicitly casts the short operands to int (or unsigned int if int cannot represent the entire range of the original type) before executing the operation.
short x = 10;
short y = 20;

// x and y are promoted to 'int' before the addition occurs.
// The resulting 'int' is then implicitly converted back to 'short' during assignment.
short z = x + y; 

// To observe the promotion:
auto result = x + y; // 'result' is deduced as 'int', not 'short'

Fixed-Width Alternatives

Because the exact byte size of short is implementation-defined (it can be 16 bits, 32 bits, or larger depending on the architecture), modern C++ development often favors the <cstdint> library when deterministic bit-width guarantees are required:
#include <cstdint>

int16_t exact_signed = -32768;   // Strictly 16-bit signed integer
uint16_t exact_unsigned = 65535; // Strictly 16-bit unsigned integer
Master C++ with Deep Grasping Methodology!Learn More