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

The `int` keyword in C designates the fundamental signed integer data type. It represents whole numbers without a fractional component and is architecturally mapped to the natural word size of the target machine's execution environment, making it the primary type for standard arithmetic operations.

```c theme={"dark"}
int uninitialized_var;
int initialized_var = 42;
signed int explicit_signed = -10; // 'signed' is implicit in 'int'
```

## Memory Size and Architecture Dependence

Unlike fixed-width types (e.g., `int32_t`), the exact byte size of an `int` is compiler- and architecture-dependent. The ISO C Standard mandates only that an `int` must be capable of holding a minimum range of values, requiring at least 16 bits (2 bytes) of storage.

* **16-bit architectures:** `int` is typically 2 bytes.
* **32-bit (ILP32) and 64-bit (LP64/LLP64) architectures:** `int` is universally implemented as 4 bytes (32 bits).

You can determine the exact size on a specific platform using the `sizeof` operator, which evaluates to a `size_t` constant expression:

```c theme={"dark"}
#include <stddef.h>

size_t int_size = sizeof(int);
```

## Value Range and Limits

Because `int` is signed by default, one bit is reserved for the sign. The values are represented in memory using **Two's Complement** binary encoding. The absolute limits of the `int` type for a given compiler are defined as macros in the `<limits.h>` standard library header.

* **16-bit implementation:**
  * Minimum (`INT_MIN`): -32,768 ($-2^{15}$)
  * Maximum (`INT_MAX`): 32,767 ($2^{15} - 1$)
* **32-bit implementation:**
  * Minimum (`INT_MIN`): -2,147,483,648 ($-2^{31}$)
  * Maximum (`INT_MAX`): 2,147,483,647 ($2^{31} - 1$)

## Integer Promotion

A fundamental semantic of C is that `int` serves as the baseline type for arithmetic evaluation. Under the rules of **Integer Promotion**, any integer type smaller than `int` (such as `char` and `short`) is automatically converted to `int` before arithmetic operations, bitwise operations, or logical evaluations are performed. If an `int` cannot represent all possible values of the original type, it is promoted to `unsigned int` instead.

```c theme={"dark"}
void demonstrate_promotion(void) {
    char a = 100;
    char b = 50;
    
    // 'a' and 'b' are implicitly promoted to 'int' before addition.
    // The result of (a + b) is of type 'int'.
    int result = a + b; 
}
```

## Signed Integer Overflow

In C, overflowing a signed integer—such as exceeding `INT_MAX` or falling below `INT_MIN`—results in **Undefined Behavior (UB)**. The compiler is permitted to optimize out overflow checks or assume overflow never occurs, which can lead to unpredictable program execution. This strictly contrasts with `unsigned int`, which safely wraps around using modulo arithmetic.

```c theme={"dark"}
#include <limits.h>

void trigger_overflow(void) {
    int max_val = INT_MAX;
    int overflowed = max_val + 1; // UNDEFINED BEHAVIOR
}
```

## Type Modifiers

The `int` base type can be altered using sign and size modifiers to adjust its memory footprint and value range. When a modifier is used, the `int` keyword itself becomes optional.

```c theme={"dark"}
unsigned int u_val = 4000000000U; // Drops the sign bit to double the positive range
short int s_val = 32000;          // Guarantees at least 16 bits
long int l_val = 2147483647L;     // Guarantees at least 32 bits
long long int ll_val = 9000000LL; // Guarantees at least 64 bits
```

## Format Specifiers

When interfacing with standard I/O functions like `printf` and `scanf`, specific format specifiers are required to correctly parse the binary data of an `int`.

* **Decimal:** `%d` and `%i` are functionally identical in `printf`. However, in `scanf`, `%d` strictly expects base-10 input, whereas `%i` will auto-detect the base if the input is prefixed with `0x` (hexadecimal) or `0` (octal).
* **Hexadecimal and Octal:** The `%x` (hexadecimal) and `%o` (octal) format specifiers strictly expect an `unsigned int`. Passing a negative `int` to these specifiers invokes Undefined Behavior. To safely inspect the binary, hexadecimal, or octal representation of a signed `int`, it must be explicitly cast to an `unsigned int`.

```c theme={"dark"}
#include <stdio.h>

void print_integers(void) {
    int val = 255;
    int negative_val = -255;

    // Standard decimal output
    printf("Decimal: %d\n", val); 
    printf("Decimal: %i\n", val);

    // Explicit cast to unsigned int is required to avoid Undefined Behavior
    printf("Hexadecimal: %x\n", (unsigned int)negative_val); 
    printf("Octal: %o\n", (unsigned int)negative_val);       
}
```

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