`register` — C Keyword

`register` — C Keyword

The register keyword in C: hints that a variable should be kept in a CPU register.

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.

register (C)

A storage class specifier that hints to the compiler that the variable should be kept in a CPU register for fast access. Taking the address of a register variable is not allowed.

Syntax

register Type name;

Example

#include <stdio.h>

long sum_array(const int* arr, int n) {
    register long sum = 0;
    for (register int i = 0; i < n; ++i) {
        sum += arr[i];
    }
    return sum;
}

int main(void) {
    int data[] = {1, 2, 3, 4, 5};
    printf("%ld\n", sum_array(data, 5));   /* 15 */

    register int x = 10;
    /* int* p = &x;   -- error: cannot take address of register variable */
    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;
}