0
0
Cprogramming~5 mins

Register storage class

Choose your learning style9 modes available
Introduction

The register storage class tells the computer to store a variable in a fast place called a CPU register. This makes the variable quicker to access.

When you want a variable to be accessed very quickly inside a function.
When you have a small variable used many times in a loop.
When you want to suggest the compiler to optimize speed for a variable.
When you want to limit the variable's scope to the function only.
When you want to save memory by not storing the variable in RAM.
Syntax
C
register int count;

The register keyword is a hint to the compiler, not a command.

You cannot get the address of a register variable using the & operator.

Examples
This declares a register variable speed inside a function.
C
void example() {
    register int speed = 10;
    speed = speed + 5;
}
A register variable of type char holding a letter.
C
register char letter = 'A';
Sample Program

This program uses a register variable counter in a loop to add numbers from 1 to 5. It then prints the sum.

C
#include <stdio.h>

int main() {
    register int counter;
    int sum = 0;

    for (counter = 1; counter <= 5; counter++) {
        sum += counter;
    }

    printf("Sum of numbers 1 to 5 is %d\n", sum);
    return 0;
}
OutputSuccess
Important Notes

Modern compilers often ignore register because they optimize variables automatically.

You cannot use register for global variables or static variables.

Summary

register suggests storing a variable in CPU registers for speed.

It is used for small, frequently used variables inside functions.

You cannot take the address of a register variable.