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

`_Complex` is a built-in type specifier introduced in C99 used to declare variables that represent complex numbers. It modifies standard floating-point types to allocate contiguous memory for both a real and an imaginary component within a single scalar variable.

## Type Variants

The `_Complex` keyword is not a standalone type. It must be explicitly combined with one of the three standard floating-point type specifiers to form a valid type. The C standard does not mandate specific bit-widths or representations (such as IEEE 754) for these underlying types.

```c theme={"dark"}
float _Complex z_float;             // Two floats
double _Complex z_double;           // Two doubles
long double _Complex z_long_double; // Two long doubles
```

## The `<complex.h>` Header

While `_Complex` is a native compiler keyword, standard C practice utilizes the `<complex.h>` header. This header provides the `complex` macro (which expands to `_Complex`) and the `I` macro.

According to the C standard, `I` expands to either `_Complex_I` or `_Imaginary_I`. It evaluates to a constant expression of type `const float _Complex` only if the implementation expands it to `_Complex_I`. If the compiler supports pure imaginary types, it expands to `_Imaginary_I`, which has the type `const float _Imaginary`.

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

int main(void) {
    // 'complex' is a macro for '_Complex'
    double complex z = 3.0 + 4.0 * I; 
    
    printf("z = %.1f + %.1fi\n", creal(z), cimag(z));
    return 0;
}
```

## Memory Layout and Alignment

The C standard guarantees that the memory layout and alignment of a `_Complex` type are strictly equivalent to an array of two elements of the corresponding real floating-point type.

* **Index 0:** The real component.
* **Index 1:** The imaginary component.

Because of this strict layout guarantee, it is safe to cast a pointer to a `_Complex` type into a pointer to its underlying real type to access the components directly.

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

int main(void) {
    double _Complex z = 5.0 + 2.0 * I;

    // Valid and strictly conforming memory access
    double *ptr = (double *)&z;
    double real_part = ptr[0]; 
    double imag_part = ptr[1]; 

    printf("Real: %.1f, Imaginary: %.1f\n", real_part, imag_part);
    return 0;
}
```

## Component Access and Initialization

While the `I` macro allows for algebraic initialization, C11 introduced the `CMPLX`, `CMPLXF`, and `CMPLXL` macros to construct complex numbers safely, avoiding edge cases with floating-point environments (like `NaN` or signed zeros).

To extract components without pointer casting, the `<complex.h>` header provides strictly typed functions. For example, `creal()` and `cimag()` take a `double complex` argument and return a `double`. Corresponding functions exist for `float` (`crealf()`, `cimagf()`) and `long double` (`creall()`, `cimagl()`). True type-generic macros for complex numbers are provided separately by the `<tgmath.h>` header.

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

int main(void) {
    // C11 standard initialization
    double complex z = CMPLX(0.0, -1.0);

    // Strictly typed component extraction
    double r = creal(z); // Takes double complex, returns double
    double i = cimag(z); // Takes double complex, returns double

    printf("r = %.1f, i = %.1f\n", r, i);
    return 0;
}
```

## Native Arithmetic

The C standard explicitly defines the semantics and behavior of standard arithmetic operators (`+`, `-`, `*`, `/`) and assignment operators (`+=`, `-=`, `*=`, `/=`) when applied to `_Complex` types. The compiler automatically generates the underlying instructions to handle complex arithmetic rules (e.g., cross-multiplication for the `*` operator).

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

int main(void) {
    double _Complex z1 = 1.0 + 2.0 * I;
    double _Complex z2 = 3.0 - 4.0 * I;

    double _Complex sum = z1 + z2; 
    double _Complex prd = z1 * z2; 

    printf("Sum: %.1f %+.1fi\n", creal(sum), cimag(sum));
    printf("Product: %.1f %+.1fi\n", creal(prd), cimag(prd));
    return 0;
}
```

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