0Pricing
C Academy · Lesson

register and const

Hints and immutability.

The register Hint

register suggests that a variable be kept in a CPU register for speed. It is only a hint; modern compilers usually ignore it.

#include <stdio.h>
int main(void) {
    register int i;
    int sum = 0;
    for (i = 0; i < 5; i++)
        sum += i;
    printf("sum = %d\n", sum);
    return 0;
}

You Cannot Take Its Address

Because a register variable might not live in memory, you may not take its address with &.

#include <stdio.h>
int main(void) {
    register int x = 10;
    printf("x = %d\n", x);
    return 0;
}

All lessons in this course

  1. auto and Local Scope
  2. static Variables
  3. extern and Global
  4. register and const
← Back to C Academy