0Pricing
C Academy · Lesson

const Variables

Make values read-only.

const Variables is a free C Academy lesson on CoddyKit — lesson 3 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, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What const Means

The const keyword tells the compiler a variable's value must not change after initialization.

If you try to modify it later, compilation fails. This protects values that should stay fixed.

const int MAX_USERS = 100;

Declaring a const

You must give a const variable its value when you declare it, because you cannot assign to it afterward.

Here PI is initialized once and used like any normal variable for reading.

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

Trying to Change a const

Assigning to a const after declaration is a compile error.

The line below would be rejected by the compiler, which is exactly the protection we want.

const int LIMIT = 50;
/* LIMIT = 60; <- compile error */

const Is Typed

Unlike a raw macro, a const variable has a real type the compiler checks.

This gives you type safety: the compiler knows RATE is a double and warns about misuse.

#include <stdio.h>
int main(void) {
    const double RATE = 0.05;
    double total = 200.0 * RATE;
    printf("%.2f\n", total);
    return 0;
}

const with Pointers: Data

Pointer const-ness has two flavors. const int *p means the data pointed to cannot be changed through p.

You can still move p to point elsewhere, but you cannot write through it.

int x = 5;
const int *p = &x;
/* *p = 9; <- error: data is const */

const with Pointers: Pointer

int *const p means the pointer itself cannot be reassigned, but the data can be changed.

Read declarations right to left: "p is a const pointer to int".

int x = 5, y = 8;
int *const p = &x;
*p = 9;       /* allowed */
/* p = &y;    <- error: pointer is const */

Protecting Function Arguments

Marking a pointer parameter const promises the function will not modify the caller's data.

This documents intent and lets the compiler catch accidental writes.

#include <stdio.h>
void show(const char *msg) {
    printf("%s\n", msg);
}
int main(void) {
    show("hello");
    return 0;
}

const in a Calculation

A common use is fixed configuration values, like a tax rate or a conversion factor.

Reading the constant keeps the formula clear and the value in one place.

#include <stdio.h>
int main(void) {
    const double KM_PER_MILE = 1.609;
    double miles = 10.0;
    printf("%.2f km\n", miles * KM_PER_MILE);
    return 0;
}

Const Has a Memory Location

Because a const is a real variable, it occupies memory and you can take its address.

This differs from a #define macro, which is pure text substitution with no address.

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

Scope and Lifetime

A const follows the same scoping rules as ordinary variables.

Declared inside a function it is local; declared outside it is global. This scoping is another advantage over file-wide macros.

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

const and Magic Numbers

Replacing raw numbers in your code with named const values removes "magic numbers".

If the value ever changes, you edit one line instead of hunting through the whole program.

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

Quick Check

Check your grasp of const behavior.

Recap

A const variable is a typed, read-only value the compiler protects from change.

It has a type, a scope, and an address, unlike a macro. With pointers, const can apply to the data, the pointer, or both. Next we will compare this with #define.

Frequently asked questions

Is the “const Variables” lesson free?

Yes — the full text of “const Variables” is free to read here on the web, and the C Academy course includes 4 lessons in total. 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.

What will I learn in “const Variables”?

Make values read-only. 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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “const Variables” 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. Defining Enums
  2. Enum Values and Ranges
  3. const Variables
  4. #define Constants
← Back to C Academy