> ## 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.

# C unsigned short

`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:** $0$ to $2^N - 1$, where $N$ is the number of bits. On standard 16-bit implementations, the range is **$0$ to $65,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:

```c theme={"dark"}
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).

```c theme={"dark"}
#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)**.

```c theme={"dark"}
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 $2^N$ only occurs during the **conversion** (or assignment) of an out-of-range value back into an `unsigned short` variable.

```c theme={"dark"}
#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;
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
