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 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.
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 00 to 2812^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.
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 2CHAR_BIT2^{\text{CHAR\_BIT}}) occurs only during the assignment or explicit conversion of the resulting int back to an unsigned char.
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.
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.
float f = 3.14f;
// Legal under C standard: inspecting the object representation of a float
unsigned char *byte_ptr = (unsigned char *)&f; 
Master C with Deep Grasping Methodology!Learn More