Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

auto: type inference and automatic storage duration

In C23, `auto` enables type inference from an initializer. With an explicit type at block scope, it retains its traditional meaning.

Version: C23; repository compiler flag, production runtime not attested

Syntax

auto identifier = expression;
auto int identifier = expression;

auto value = 21; infers int at compile time. The type does not change dynamically. In contrast, auto int twice = 42; at block scope explicitly specifies automatic storage duration—it does not infer the type.

For inference, declare one object with an expression initializer, not a {...} list. The initializer cannot use the declared name. For portability, write auto pointer = &value;, not auto *pointer = &value;.

Inference applies lvalue, array-to-pointer, and function-to-pointer conversions. static auto is possible; auto therefore does not itself force automatic storage duration.

Examples

#include <stdio.h>

int main(void)
{
    auto value = 21;
    auto pointer = &value;
    auto int twice = *pointer * 2;
    printf("%d %d\n", value, twice);
    return 0;
}

Result

21 42

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources