Skip to content
Tutorials Center
My learning

Topics

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.

C
register int i;      /* cannot use &i */
auto n = 42;         /* C23: n is int */

Try it

C auto type inference (C23)

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.

C register and the address-of rule

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

C
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

  • register is 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.
  • register still matters for one thing: compilers reject &x on 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 plain int unless you write auto const m = k;.
  • Character constants are int in C, so auto ch = 'x'; is an int; write char ch = 'x'; if you want a char. Likewise, floating literals deduce double and integer literals int unless 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). Use typeof in GNU C if you need the idea earlier.
  • Other storage classes: static, extern, thread_local, constexpr.