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.

The char data type in C is an integral type representing the smallest addressable unit of memory, guaranteed by the C Standard to be exactly one byte (sizeof(char) == 1). While semantically intended for character representation via encodings like ASCII, it is fundamentally an integer type that stores numeric values.

Memory Representation

The exact number of bits in a char is defined by the CHAR_BIT macro in <limits.h>. On virtually all modern architectures, CHAR_BIT is 8, meaning a char occupies 8 bits.
#include <stdio.h>
#include <limits.h>

int main(void) {
    printf("Size of char: %zu byte(s)\n", sizeof(char));
    printf("Bits per char: %d\n", CHAR_BIT);
    return 0;
}

Signedness and Variants

C defines three distinct character types. Unlike int, where int and signed int are strictly identical, plain char is treated as a unique type by the compiler, though it will share the representation and range of either its signed or unsigned counterpart based on the compiler implementation and target architecture.
  1. char: The default character type. Its signedness is implementation-defined.
  2. signed char: Guaranteed to be able to hold negative values. Assuming an 8-bit byte using two’s complement, its range is -128 to 127 (SCHAR_MIN to SCHAR_MAX).
  3. unsigned char: Guaranteed to hold only non-negative values. Assuming an 8-bit byte, its range is 0 to 255 (0 to UCHAR_MAX).
char default_char = 'A';           // Signedness depends on compiler/architecture
signed char negative_val = -50;    // Explicitly signed
unsigned char raw_byte = 250;      // Explicitly unsigned

Literals and Initialization

Character literals are enclosed in single quotes. When the compiler encounters a character literal, it translates it to its corresponding integer value based on the execution character set.
char a = 'A';          // Initializes 'a' with the integer value 65 (in ASCII)
char b = 65;           // Exactly equivalent to the line above
char newline = '\n';   // Escape sequence translated to integer 10
char hex_val = '\x41'; // Initialized using a hexadecimal escape sequence
Note: In C, a character literal like 'A' actually has the type int, not char. When assigned to a char variable, the int value is implicitly converted to char.

Integer Promotion

Because char is an integral type with a lower conversion rank than int, it is subject to integer promotion. When a char is used in an arithmetic expression, it is automatically promoted to an int (or unsigned int if int cannot represent all values of the original type) before the operation is performed.
char x = 10;
char y = 20;

// x and y are promoted to int before addition. 
// The result is an int, which is then converted if assigned back to a char.
int result = x + y; 

Out-of-Range Conversion Behavior

Because arithmetic operations on char variables are performed in the int domain due to integer promotion, arithmetic overflow rarely occurs during the operation itself. Instead, boundary behaviors manifest during the integer conversion when the int result is assigned back to the narrower char type.
  • unsigned char: Converting an out-of-range integer to an unsigned type follows modulo arithmetic. The value is reduced modulo UCHAR_MAX + 1.
  • signed char: Converting an out-of-range integer to a signed type cannot be represented by the target type. According to the C standard (pre-C23), this results in Implementation-Defined Behavior (or raises an implementation-defined signal). As of C23, this conversion is well-defined to wrap around using two’s complement.
unsigned char u = 255;
// 255 + 1 evaluates to 256 of type int (no arithmetic overflow).
// Converting 256 to unsigned char yields 256 % 256 = 0.
u = u + 1; 

signed char s = 127;
// 127 + 1 evaluates to 128 of type int (no arithmetic overflow).
// Converting 128 to signed char is Implementation-Defined Behavior (pre-C23)
// or well-defined to wrap to -128 (C23+).
s = s + 1; 
Master C with Deep Grasping Methodology!Learn More