- What the
registerstorage class does — and what it does not do - Why
registeris a hint, not a command - How to time code with
clock()andCLOCKS_PER_SEC - Why modern compilers generate identical code with or without
register - When (if ever)
registerstill makes a difference
What is the register Storage Class?
The register storage class is a suggestion to the compiler that a variable will be accessed so frequently that it should be kept in a CPU register rather than in memory. Declaring register int i; tells the compiler: "if you have a spare register, keep i there".
In practice, modern optimizing compilers ignore this hint entirely. They analyze the actual usage and put the hottest variables in registers on their own — often better than a human would. The keyword remains part of the language but is largely ceremonial.
💡 Quick Definition: register is a request to store a variable in a CPU register. It's not a guarantee. If the compiler can't honor it — no spare register, or the address is taken — the variable silently becomes an ordinary auto variable.
Key Rules
| Property | Value |
|---|---|
| Storage | CPU register if possible, otherwise stack |
| Default value | Garbage (uninitialized) |
| Scope | Inside the enclosing block |
| Lifetime | Block duration |
| Address taken? | Illegal — &x is a compile error |
| Used at file scope? | Illegal — register is for block-scope declarations only |
Complete C Program
Program: Time a hot loop with and without register
#include <stdio.h>
#include <time.h>
#define N 50000000L /* 50 million iterations */
/* Version 1: ordinary auto counter */
long long sumStandard(void) {
long long sum = 0;
long i;
for (i = 0; i < N; i++) {
sum += i * i;
}
return sum;
}
/* Version 2: register counter (hint only) */
long long sumRegister(void) {
long long sum = 0;
register long i;
for (i = 0; i < N; i++) {
sum += i * i;
}
return sum;
}
int main(void) {
clock_t t1, t2;
double secondsStandard, secondsRegister;
long long s1, s2;
/* -------- Standard variable run -------- */
t1 = clock();
s1 = sumStandard();
t2 = clock();
secondsStandard = (double)(t2 - t1) / CLOCKS_PER_SEC;
/* -------- register variable run -------- */
t1 = clock();
s2 = sumRegister();
t2 = clock();
secondsRegister = (double)(t2 - t1) / CLOCKS_PER_SEC;
printf("--- Performance comparison (N = %ld) ---\n", N);
printf("Standard int counter : %.6f seconds (sum = %lld)\n",
secondsStandard, s1);
printf("register long counter: %.6f seconds (sum = %lld)\n",
secondsRegister, s2);
return 0;
}
Program Output
--- Performance comparison (N = 50000000) ---
Standard int counter : 0.142311 seconds (sum = 41666666666666675000)
register long counter: 0.141892 seconds (sum = 41666666666666675000)
The two times are essentially identical — typically within measurement noise. On many runs the register version is even slightly slower, simply because of timing jitter. Both loops compute the same result.
Step-by-Step Explanation
Let's break down what the program does and why the timings come out the way they do.
1. Include headers and define N:
#include <stdio.h>
#include <time.h>
#define N 50000000L
stdio.h provides printf; time.h provides clock() and CLOCKS_PER_SEC. N is the loop count — 50 million, large enough that timing is meaningful.
2. Function with ordinary counter:
long long sumStandard(void) {
long long sum = 0;
long i;
for (i = 0; i < N; i++)
sum += i * i;
return sum;
}
The loop counter i is a plain auto variable. The compiler is free to store it on the stack or in a register.
3. Function with register counter:
long long sumRegister(void) {
long long sum = 0;
register long i;
for (i = 0; i < N; i++)
sum += i * i;
return sum;
}
Same code, but i is declared register. The compiler now has an extra incentive to keep i in a register — but in practice, it would have done that anyway.
4. Time the standard version:
t1 = clock();
s1 = sumStandard();
t2 = clock();
secondsStandard = (double)(t2 - t1) / CLOCKS_PER_SEC;
clock() returns the number of CPU clock ticks since program start. Dividing the difference by CLOCKS_PER_SEC converts it to seconds.
5. Time the register version the same way:
t1 = clock();
s2 = sumRegister();
t2 = clock();
secondsRegister = (double)(t2 - t1) / CLOCKS_PER_SEC;
Same measurement pattern — the variable in question has zero effect on the timing logic.
6. Why the times match:
- Modern optimizers (GCC -O2, Clang -O2, MSVC) automatically allocate hot loop variables to registers.
- The
registerkeyword is a hint the compiler may ignore. - Both functions produce the exact same optimized machine code.
- Any difference in the printed times is measurement jitter, not a real speedup.
🎯 Key Takeaway: In modern C, writing register is a no-op — the compiler's optimizer makes better choices than any human hint. The keyword is kept only for backward compatibility and for teaching. If you want real performance, focus on algorithm choice and data layout, not on register.
Frequently Asked Questions
1. Does register actually speed anything up?
Rarely, and never on modern compilers. The classic case where it mattered was 1970s compilers that didn't perform register allocation automatically. Today, GCC/Clang/MSVC ignore the hint and use their own optimizer — which is almost always correct.
2. Can I take the address of a register variable?
No. &x where x is register is a compile error — a CPU register has no memory address. If you need the address, the compiler will silently demote the variable to auto.
3. Can register be used with global variables?
No. register is only valid for block-scope declarations inside a function. Writing register int x; at file scope is a compile error.
4. Is register deprecated in C?
Formally, no — it's still in the standard. But it's effectively dead. C++17 actually removed the register keyword. In C it survives only because old code still uses it.
5. How can I really measure performance differences?
Use -O2 or -O3, then examine assembly with objdump -d or godbolt.org. If the assembly is identical (which it almost always will be), the timings are just noise. For real profiling, use perf, valgrind --tool=callgrind, or gprof.
💡 Tip: To see whether register changed anything at all, compile both functions and compare their disassembly. Almost certainly they will be byte-for-byte identical.