![]() |
VOOZH | about |
The register storage class is used to suggest that a variable should be stored in a CPU register instead of main memory for faster access. Since registers are faster than memory, register variables are generally used for frequently accessed data.
A register variable may be stored in a CPU register instead of memory. Therefore, using the address-of (&) operator with a register variable is not allowed.
Output
./Solution.c: In function 'main':
./Solution.c:7:5: error: address of register variable 'i' requested
7 | int *ptr = &i;
| ^~~
A pointer variable itself can be declared as a register variable because the memory address stored by the pointer can reside in a CPU register.
10
The register and static keywords are both storage class specifiers. Since a variable can have only one storage class, they cannot be used together.
Output
./Solution.c: In function 'main':
./Solution.c:5:5: error: multiple storage classes in declaration specifiers
5 | register static int x = 10;
| ^~~~~~~~
The register storage class can only be applied to local variables declared inside a function or block. It cannot be used for global variables.
Output
error: register name not specified for 'x'
C does not impose a limit on the number of register variables that can be declared in a program. However, the compiler decides which variables are actually stored in CPU registers based on the available registers and optimization settings.