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
- auto and Local Scope
- static Variables
- extern and Global
- register and const