liveBook · Manning

Register keyword

As explained in Modern C, Third Edition

register is a C storage-class specifier that requests automatic storage for an object and permits implementation-specific restrictions on taking its address.

The C register storage-class specifier declares an object with automatic storage duration and tells the implementation that its address must not be taken. Most such local objects live from entry into their defining block until execution leaves it; recursive calls create separate instances. section 13.2.2

A restricted local object

register int counter; counter++; /* &counter is not permitted */ The declaration can help the compiler reason that counter cannot alias another object, because the address operator is forbidden for register objects. section 13.2.2

Key rules:

  • Use register for local, non-array variables in performance-critical code when preventing address-taking is acceptable. section 13.2.2
  • Do not apply the address operator to a register object; the declaration makes that operation invalid. section 13.2.2
  • Treat arrays separately because they decay to the address of their first element in almost all contexts. section 13.2.2
  • Avoid taking addresses of local variables in complex code when optimization and aliasing are concerns. section 16.0

How Modern C, Third Edition covers it

How deep it goes
Explains automatic lifetimes, recursion, address restrictions, aliasing, and array complications, but does not develop compiler implementation details.
Examples
Uses local variables, recursive calls, non-array performance-critical variables, and arrays whose elements undergo address decay.
What it assumes
Assumes familiarity with C local variables, scopes, pointers, arrays, recursion, and compiler optimization.
What you can do after
Apply register to selected local variables while preserving address-taking constraints and accounting for array behavior.
How it connects
Connects register with automatic storage duration, recursion, aliasing, arrays, variable-length arrays, and optimization.

Also explained in

Compare how 2 books explain register keyword →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.