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

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.

```c theme={"dark"}
#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`).

```c theme={"dark"}
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.

```c theme={"dark"}
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.

```c theme={"dark"}
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.

```c theme={"dark"}
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; 
```

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