> ## 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, non-negative integer type in C++ that occupies exactly one byte of memory. Unlike the standard `char` type—which has implementation-defined signedness—`unsigned char` is explicitly guaranteed to represent only positive values and zero, utilizing all available bits for magnitude rather than reserving a bit for a sign.

## Memory Representation and Range

The C++ standard guarantees that `sizeof(unsigned char)` is exactly `1`. The number of bits in this byte is defined by the `CHAR_BIT` macro (found in `<climits>`), which is 8 on all modern architectures. Furthermore, the standard guarantees that `unsigned char` has no padding bits; every bit contributes directly to the value.

Because it lacks a sign bit, an 8-bit `unsigned char` can represent $2^8$ distinct values.

* **Minimum value:** `0`
* **Maximum value:** `255` (`UCHAR_MAX`)

```cpp theme={"dark"}
#include <iostream>
#include <limits>

int main() {
    unsigned char min_val = std::numeric_limits<unsigned char>::min(); // 0
    unsigned char max_val = std::numeric_limits<unsigned char>::max(); // 255
    
    std::cout << "Size: " << sizeof(unsigned char) << " byte\n";
    return 0;
}
```

## Strict Aliasing Exemption

At the compiler level, `unsigned char` holds a unique, privileged status regarding C++ strict aliasing rules. The standard explicitly permits an `unsigned char*` (along with `char*` and `std::byte*`) to alias any other object type in memory. Casting an arbitrary object's pointer to an `unsigned char*` to inspect its raw memory representation is strictly well-defined and will not invoke undefined behavior.

```cpp theme={"dark"}
float value = 3.14f;

// Legal under C++ strict aliasing rules:
unsigned char* byte_pointer = reinterpret_cast<unsigned char*>(&value);
```

## Integer Promotion and Wrap-Around Behavior

Although `unsigned char` is an integer type, it is subject to integral promotion during arithmetic and bitwise operations. When an `unsigned char` is used in an expression, the compiler implicitly promotes it to a standard `int` (assuming `int` can represent all values of `unsigned char`, which is universally true on 32-bit and 64-bit systems) before evaluating the operation.

Because of this promotion, arithmetic operations on `unsigned char` evaluate as `int` and do *not* natively wrap around during the evaluation of the expression. Wrap-around modulo $2^{CHAR\_BIT}$ (typically 256) only occurs when the result is converted or assigned back to an `unsigned char`. This assignment wrap-around is strictly well-defined and never triggers undefined behavior.

```cpp theme={"dark"}
unsigned char a = 255;
unsigned char b = 1;

// 'a' and 'b' are promoted to 'int' before addition.
// The type of 'result' is 'int', and its value is 256 (no wrap-around).
auto result = a + b; 

// Wrap-around occurs during assignment back to unsigned char.
// 256 modulo 256 evaluates to 0.
unsigned char wrapped_result = a + b; 

unsigned char y = 0;
// 0 - 1 evaluates to -1 (int). 
// Assigning -1 to unsigned char wraps around to 255.
y = y - 1; 
```

## Standard I/O Stream Interaction

When interacting with standard I/O streams, `unsigned char` is treated as a character type rather than a numeric integer. Passing an `unsigned char` to `std::cout` will output its corresponding character representation from the execution character set, not its numeric value. To output the underlying integer value, the `unsigned char` must be explicitly cast to a standard integer type.

```cpp theme={"dark"}
#include <iostream>

int main() {
    unsigned char val = 65;
    
    // Treats 'val' as a character. Prints 'A' (assuming ASCII).
    std::cout << val << "\n"; 
    
    // Casts to int to print the numeric value. Prints '65'.
    std::cout << static_cast<int>(val) << "\n"; 
    
    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>
