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

An `unsigned char` is a fundamental integer data type in C that occupies exactly one byte of memory and represents strictly non-negative values. By omitting the sign bit present in a `signed char`, all available bits are dedicated to representing the magnitude of the numeric value.

```c theme={"dark"}
unsigned char value = 255;
```

## Memory and Size Guarantees

The C standard mandates that `sizeof(unsigned char)` is always exactly `1`. The actual number of bits in this byte is determined by the `CHAR_BIT` macro defined in `<limits.h>`. On virtually all modern architectures, `CHAR_BIT` is 8.

## Value Range

Because it lacks a sign bit, the range of an `unsigned char` is strictly non-negative:

* **Minimum:** `0`
* **Maximum:** `UCHAR_MAX` (defined in `<limits.h>`)

For a standard 8-bit byte, the range is calculated as $0$ to $2^8 - 1$, resulting in a range of `0` to `255`.

## Integer Promotion

When an `unsigned char` is used in an arithmetic expression or passed to a variadic function, it is subject to **integer promotion**. Before the operation occurs, the `unsigned char` is implicitly converted to an `int` provided that `int` can represent all values of `unsigned char` (i.e., `INT_MAX >= UCHAR_MAX`). If `int` cannot represent all values—which can occur on systems where `sizeof(int) == 1` and `CHAR_BIT` is large, making `UCHAR_MAX > INT_MAX`—it is instead promoted to `unsigned int`.

```c theme={"dark"}
unsigned char a = 100;
unsigned char b = 200;
// Assuming INT_MAX >= UCHAR_MAX, 'a' and 'b' are promoted to 'int' before addition.
// The result (300) is an 'int', which is then truncated if assigned back to an unsigned char.
int result = a + b; 
```

## Arithmetic Mechanics and Wrap-Around

Due to integer promotion, arithmetic operations involving `unsigned char` are typically performed as signed `int`. Consequently, `unsigned char` arithmetic does not inherently adhere to unsigned modulo arithmetic during expression evaluation. The wrap-around behavior (modulo $2^{\text{CHAR\_BIT}}$) occurs only during the *assignment* or explicit conversion of the resulting `int` back to an `unsigned char`.

```c theme={"dark"}
unsigned char max = 255;
// 'max' is promoted to int (255). 255 + 1 = 256 (int).
// Assignment converts 256 back to unsigned char, wrapping around to 0.
max = max + 1; 

unsigned char min = 0;
// 'min' is promoted to int (0). 0 - 1 = -1 (int).
// Assignment converts -1 back to unsigned char, wrapping around to 255.
min = min - 1; 
```

Because the intermediate arithmetic is performed as a signed `int`, chained operations can cause signed integer overflow, resulting in Undefined Behavior (UB) if the intermediate value exceeds `INT_MAX`.

```c theme={"dark"}
unsigned char c = 250;
// 'c' is promoted to int. 250 * 250 * 250 * 250 = 3,906,250,000.
// On a system with 32-bit ints (INT_MAX = 2,147,483,647), this causes UB
// before the final assignment can truncate the value.
unsigned char result = c * c * c * c; 
```

## Strict Aliasing Exception

At the compiler level, `unsigned char` holds a unique position regarding C's strict aliasing rules. The C standard explicitly permits an `unsigned char *` to alias any other pointer type. This means a pointer to an `unsigned char` can legally be used to access the raw object representation (the underlying bytes) of any variable in memory without violating strict aliasing and invoking undefined behavior.

```c theme={"dark"}
float f = 3.14f;
// Legal under C standard: inspecting the object representation of a float
unsigned char *byte_ptr = (unsigned char *)&f; 
```

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