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.

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 282^8 distinct values.
  • Minimum value: 0
  • Maximum value: 255 (UCHAR_MAX)
#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.
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 2CHAR_BIT2^{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.
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.
#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;
}
Master C++ with Deep Grasping Methodology!Learn More