C Reference ยท Keywords and Statements
The register and auto keywords
Reference for the C register and auto keywords: the legacy storage class specifiers, why register forbids taking an address, and auto type inference in C23.
register and auto are storage class specifiers that date from the earliest C. register hints that a local variable should live in a CPU register; its only lasting effect is that you may not take the variable's address. auto says a local has automatic storage, which is already the default, so it was never written in practice.
C23 gave auto a new job: type inference. When a variable is declared with auto and an initializer, its type is deduced from the initializer, much like C++ auto. GCC 15.2 in C23 mode accepts it.
register int i; /* cannot use &i */ auto n = 42; /* C23: n is int */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
register type name; auto name = initializer; /* C23: type inferred */ auto type name; /* all standards: automatic storage */
| Form | Meaning |
|---|---|
register |
Suggests register storage. Applies to block-scope variables and parameters. The address cannot be taken. |
auto (old) |
Automatic storage duration. Redundant. |
auto x = e; (C23) |
Type of x is the type of e after array-to-pointer and function-to-pointer conversion, with qualifiers dropped. |
Errors: &x on a register object is a constraint violation. A C23 auto declaration needs an initializer, may not declare several names with different deduced types, and the initializer cannot be a braced list or refer to the variable itself.
Usage notes
registeris obsolete. Compilers allocate registers better than humans. It is deprecated in C++ and removed there in C++17, which matters for code shared between both.registerstill matters for one thing: compilers reject&xon such a variable, which catches accidental aliasing in hot loops.- Inference drops qualifiers of the initializer's top level.
const int k = 1; auto m = k;gives a plainintunless you writeauto const m = k;. - Character constants are
intin C, soauto ch = 'x';is anint; writechar ch = 'x';if you want achar. Likewise, floating literals deducedoubleand integer literalsintunless suffixed. - Use inference sparingly. It helps with long type names (
auto it = make_table(...)), but hides the type at a glance; an explicit type is clearer for numeric code. - Pre-C23 compilers treat
auto x = 5;as an implicit-int declaration (error in modern modes). Usetypeofin GNU C if you need the idea earlier. - Other storage classes:
static,extern,thread_local,constexpr.
Related
typeof,static,constexpr, storage duration- Lessons: Scope and storage classes, Modern C