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.

unsigned short (synonymous with unsigned short int) is a fundamental integer data type in C that represents strictly non-negative whole numbers. By omitting the sign bit required in signed integers, it allocates all available memory bits to represent the magnitude of the value, effectively doubling the maximum representable positive limit compared to its signed counterpart.

Technical Specifications

  • Memory Size: The C Standard (ISO/IEC 9899) guarantees that a short must be at least 16 bits (2 bytes) in size. Its size must be less than or equal to the size of an int.
  • Value Range: 00 to 2N12^N - 1, where NN is the number of bits. On standard 16-bit implementations, the range is 00 to 65,53565,535.
  • Standard Limits: The exact maximum value for the target architecture is defined by the USHRT_MAX macro, located in the <limits.h> header.

Syntax and Declaration

The int keyword is implicit and can be omitted. Both declarations below are semantically identical:
unsigned short value_one = 65000;
unsigned short int value_two = 65000;

Format Specifiers

When interfacing with standard I/O functions like printf and scanf, the unsigned short type requires the %hu format specifier (h for half/short, u for unsigned).
#include <stdio.h>

int main(void) {
    unsigned short my_val = 42000;
    
    // Outputting the value
    printf("Value: %hu\n", my_val);
    
    // Reading into the value
    scanf("%hu", &my_val);
    
    return 0;
}

Integer Promotion

In C, unsigned short is subject to strict integer promotion rules. When an unsigned short is used as an operand in almost any arithmetic or bitwise expression (using operators like +, -, *, /, ~), it is implicitly promoted before the operation is evaluated. This promotion occurs regardless of the other operand’s type.
  • If the standard int type can represent all possible values of unsigned short (which is true on typical systems where int is 32-bit and short is 16-bit), the unsigned short operand is promoted to a signed int.
  • If int and short are the same size (e.g., both are 16-bit), the operand is promoted to unsigned int.
For example, adding two unsigned short variables together will promote both to int before the addition takes place, yielding an int result.

Arithmetic and Overflow Behavior

Because unsigned short operands are typically promoted to signed int during evaluation, arithmetic operations do not strictly operate using unsigned modulo arithmetic. If the result of the promoted signed int arithmetic overflows INT_MAX, it results in Undefined Behavior (UB).
unsigned short a = 65535;

// If int is 32-bit, 65535 * 65535 = 4294836225.
// This exceeds INT_MAX (2147483647), causing signed integer overflow (UB).
// It does NOT safely wrap around.
int result = a * a; 
Well-defined wrap-around modulo 2N2^N only occurs during the conversion (or assignment) of an out-of-range value back into an unsigned short variable.
#include <stdio.h>
#include <limits.h>

int main(void) {
    unsigned short max_val = USHRT_MAX; // Typically 65535
    
    // The expression (max_val + 1) evaluates to 65536 of type int.
    // The wrap-around to 0 occurs during the assignment conversion back to unsigned short.
    max_val = max_val + 1; 
    
    // The expression (0 - 1) evaluates to -1 of type int.
    // The assignment conversion wraps the -1 modulo 2^N, resulting in USHRT_MAX.
    unsigned short min_val = 0;
    min_val = min_val - 1; 
    
    return 0;
}
Master C with Deep Grasping Methodology!Learn More