Storage Class #2: register
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program to Compare the Performance of register vs Standard Variables

📑 On this page:
  • What is the register Storage Class?
  • Key Rules
  • Complete C Program
  • Program Output
  • Step-by-Step Explanation
  • Frequently Asked Questions
📚 In this program, you will learn:
  • What the register storage class does — and what it does not do
  • Why register is a hint, not a command
  • How to time code with clock() and CLOCKS_PER_SEC
  • Why modern compilers generate identical code with or without register
  • When (if ever) register still 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
StorageCPU register if possible, otherwise stack
Default valueGarbage (uninitialized)
ScopeInside the enclosing block
LifetimeBlock 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 register keyword 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.

📖 Related Tutorials

  • Storage Classes in C — Full Tutorial
  • 1. auto Storage Class
  • 3. static Storage Class State
  • 4. extern Storage Class Across Files
  • All Storage Class Assignments

Previous Topic: -->> 1. auto Storage Class   ||   Next topic: -->> 3. static Storage Class State


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.