C auto and register

Beginner
6 min read
Updated: Nov 2026
1 example

What Is auto and register?

auto is the default for variables declared inside a block, so it is almost never written. register suggests that a variable be kept in a CPU register for speed. Modern compilers allocate registers better than humans, so register is mostly historical. You cannot take the address of a register object.

Remember
auto int total = 0;       /* same as: int total = 0; */
register int i;           /* hint only */
for (register int j = 0; j < n; j++) { /* ... */ }

Cheat Sheet

Keep these facts handy while you work with auto and register in C.

TopicDetail
autoAutomatic duration, block scope
registerHint; compiler may ignore it
Address& on register variables is illegal
TodayUse optimization flags instead

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: static and extern.

Example 1 — A register loop counter

Functionally identical to an ordinary int.

C
#include <stdio.h>

int main(void) {
    auto int sum = 0;
    register int i;

    for (i = 0; i < 100; i++) {
        sum += i;
    }
    printf("sum = %d\n", sum);
    return 0;
}

How It Works

  1. auto changes nothing; locals are automatic anyway.
  2. register suggests i should live in a register.
  3. The loop adds 0 through 99, which totals 4950.

Edge Cases & Pitfalls

Check these before relying on auto and register in real programs.

addr

Taking an address

Writing &i for a register variable fails to compile.

c++

Different meaning in C++

In C++11 auto deduces a type. Do not mix the two meanings.

Key Takeaways

  • auto: implicit for locals.
  • register: only a hint.
  • Modern: trust the optimizer.

One line: auto is the default for variables declared inside a block, so it is almost never written.

Frequently Asked Questions

Rarely. Optimizing compilers decide register allocation themselves, and register can even be ignored. Enable -O2 instead.
Because it might not live in memory at all, so it may have no address.
Continue with "C static and extern" next. Persistent static locals, file-private names, and sharing globals with extern. The C Functions hub lists every topic in order.

Next: static and extern

Persistent static locals, file-private names, and sharing globals with extern.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful