"register" keyword in C

In C programming, the register keyword is a storage class specifier that suggests to the compiler to store a variable in CPU registers instead of main memory. Register variables provide faster access since CPU registers are much faster than memory. However, taking the address of a register variable is not allowed.

Syntax

register data_type variable_name;

Properties of Register Variables

  • Scope: Local to the function in which they are declared
  • Default value: Garbage value (uninitialized)
  • Lifetime: Until the end of the block execution
  • Storage: CPU registers (compiler's choice)
  • Address: Cannot use address-of operator (&) with register variables

Example 1: Basic Register Variables

This example demonstrates the declaration and usage of register variables −

#include <stdio.h>

int main() {
    register char x = 'S';
    register int a = 10;
    auto int b = 8;
    
    printf("The value of register variable x: %c<br>", x);
    printf("The sum of auto and register variable: %d<br>", (a + b));
    
    return 0;
}
The value of register variable x: S
The sum of auto and register variable: 18

Example 2: Register Pointer Variables

Register keyword can also be used with pointers. The pointer itself is stored in a register −

#include <stdio.h>

int main() {
    int i = 10;
    register int *a = &i;
    
    printf("The value pointed by register pointer: %d<br>", *a);
    printf("The address stored in register pointer: %p<br>", (void*)a);
    
    return 0;
}
The value pointed by register pointer: 10
The address stored in register pointer: 0x7fff5fbff6ac

Important Notes

  • Modern compilers often ignore the register keyword and perform their own optimization
  • You cannot take the address of a register variable using the & operator
  • Only a limited number of variables can be stored in registers
  • Register storage is faster but limited in size

Conclusion

The register keyword in C suggests faster storage for frequently used variables. While modern compilers handle optimization automatically, understanding register storage helps in writing efficient code and understanding variable storage classes.

Updated on: 2026-03-15T09:53:20+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements