> ## 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 unsigned long

`unsigned long` (formally `unsigned long int`) is a fundamental integer data type in C that represents non-negative whole numbers. It modifies the standard integer type by removing the sign bit (allocating all bits to magnitude) and guaranteeing a minimum storage size of at least 32 bits, as defined by the ISO/IEC 9899 C standard.

## Syntax and Declaration

The `int` keyword is optional when declaring an `unsigned long`. Both declarations below are semantically identical and recognized by the compiler:

```c theme={"dark"}
unsigned long var1;
unsigned long int var2; 
```

## Memory Size and Architecture Dependence

The C standard dictates minimum sizes rather than exact sizes. While `unsigned long` is guaranteed to be $\ge$ 32 bits, its actual byte size is heavily dependent on the compiler's data model and the target architecture:

* **ILP32 (32-bit systems):** 32 bits (4 bytes).
* **LLP64 (64-bit Windows):** 32 bits (4 bytes).
* **LP64 (64-bit Unix/Linux/macOS):** 64 bits (8 bytes).

To determine the exact size on a specific system at runtime, use the `sizeof` operator:

```c theme={"dark"}
size_t size = sizeof(unsigned long);
```

## Value Range

Because the type is `unsigned`, the Most Significant Bit (MSB) is not used as a sign flag. The range is always $0$ to $2^N - 1$, where $N$ is the number of bits.

* **32-bit implementation:** $0$ to $4,294,967,295$
* **64-bit implementation:** $0$ to $18,446,744,073,709,551,615$

The absolute maximum value for the compilation environment is exposed via the `ULONG_MAX` macro, which requires including the `<limits.h>` header.

## Literals

When assigning hardcoded numeric constants to an `unsigned long`, append the `UL` or `ul` suffix. This explicitly instructs the compiler to treat the literal as an `unsigned long`, preventing accidental truncation or signed integer overflow during compilation.

```c theme={"dark"}
unsigned long dec_val = 4294967295UL;
unsigned long hex_val = 0xFFFFFFFFUL;
unsigned long oct_val = 037777777777UL;
```

## Format Specifiers

When reading or writing `unsigned long` values using standard I/O functions (like `printf` or `scanf`), the `%lu` format specifier is mandatory. Using `%d` or `%u` results in undefined behavior due to type mismatch and potential stack misalignment.

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

int main(void) {
    // Value fits within the guaranteed 32-bit minimum
    unsigned long val = 3141592653UL; 
    
    // Outputting the value
    printf("Value: %lu\n", val);
    
    // Reading the value
    scanf("%lu", &val);
    
    return 0;
}
```

## Type Conversion and Promotion

Under C's usual arithmetic conversions, if an operation involves an `unsigned long` and a smaller integer type (like `int` or `unsigned int`), the smaller type is implicitly promoted to `unsigned long` before the operation is evaluated. If mixed with a signed type of the same size (e.g., `long` and `unsigned long`), the signed type is implicitly converted to `unsigned long`, which can alter the mathematical value if the signed integer is negative.

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