C Academy · Lesson

register and const

Hints and immutability.

Lesson 4 of 413 steps

register and const is a free C Academy lesson on CoddyKit. This is lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.

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;
}

Why register Is Rare Today

Optimizing compilers already place hot variables in registers. The keyword is mostly historical now.

#include <stdio.h>
int main(void) {
    int product = 1;
    for (register int i = 1; i <= 4; i++)
        product *= i;
    printf("product = %d\n", product);
    return 0;
}

The const Qualifier

const makes a variable read-only after initialization. Trying to change it is a compile error.

#include <stdio.h>
int main(void) {
    const int DAYS = 7;
    printf("DAYS = %d\n", DAYS);
    return 0;
}

Constants Document Intent

Using const for fixed values makes code clearer and protects against accidental changes.

#include <stdio.h>
int main(void) {
    const double PI = 3.14159;
    double r = 2.0;
    printf("area = %.2f\n", PI * r * r);
    return 0;
}

const With Pointers

const int *p means the pointed-to value cannot change through p. The pointer itself can still move.

#include <stdio.h>
int main(void) {
    int x = 5;
    const int *p = &x;
    printf("*p = %d\n", *p);
    return 0;
}

Constant Pointer

int * const p means the pointer cannot change, but the value it points to can.

#include <stdio.h>
int main(void) {
    int x = 5;
    int * const p = &x;
    *p = 9;
    printf("x = %d\n", x);
    return 0;
}

const Function Parameters

Marking a parameter const promises the function will not modify it, which helps callers reason about behavior.

#include <stdio.h>
void show(const int n) {
    printf("n = %d\n", n);
}
int main(void) {
    show(42);
    return 0;
}

Immutable by Design

Prefer const for any value that should not change. It catches bugs at compile time.

#include <stdio.h>
int main(void) {
    const int LIMIT = 100;
    int x = 90;
    if (x < LIMIT)
        printf("under limit\n");
    return 0;
}

Combining const and Globals

A global const is a safe, shared constant available to the whole file.

#include <stdio.h>
const int VERSION = 2;
int main(void) {
    printf("VERSION = %d\n", VERSION);
    return 0;
}

register and const Together

You can even mark a loop counter both ways, though const here means the loop body must not change it.

#include <stdio.h>
int main(void) {
    int sum = 0;
    for (int i = 0; i < 3; i++) {
        const int doubled = i * 2;
        sum += doubled;
    }
    printf("sum = %d\n", sum);
    return 0;
}

Quick Check

Test your understanding of register and const.

Recap: register and const

You learned two qualifiers.

  • register hints at register storage, but is usually ignored
  • You cannot take the address of a register variable
  • const makes a value read-only
  • Pointer constness has two flavors: const value vs const pointer
  • Use const generously for safety
Free to start

Learn C with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
39
Lessons
144

Frequently Asked Questions

Is the “register and const” lesson free?

Yes — the full text of “register and const” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.

What will I learn in “register and const”?

Hints and immutability. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “register and const” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. auto and Local Scope
  2. static Variables
  3. extern and Global
  4. register and const
← Back to C Academy