`auto` — C Keyword

`auto` — C Keyword

The auto keyword in C: a storage class specifier for automatic (stack) storage duration.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

auto (C)

Specifies automatic storage duration — the variable lives on the stack for the duration of its enclosing block. This is the default for local variables; the keyword is redundant and rarely written explicitly.

Syntax

auto Type name;
auto Type name = initializer;

Example

#include <stdio.h>

int main(void) {
    auto int x = 42;   /* same as: int x = 42; */
    printf("%d\n", x);

    {
        auto int y = 10;   /* y exists only inside this block */
        printf("%d\n", y);
    }
    /* y is no longer accessible here */
    return 0;
}

Notes

Example in practice

int main() {
    // Pick one facility from this reference page.
    // Write the smallest program that exercises its main precondition,
    // complexity rule, or lifetime constraint before scaling up.
    return 0;
}