Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · C Programming · 03 Jul 2026 · Hindi + English

Constant Variable in C: Declaration, Initialization and Rules

A constant variable in C is declared with const and must be initialized at declaration. Learn all rules, common compiler errors and correct usage.

What is a constant variable?

A constant variable in C is a variable declared with the const qualifier. It behaves like a normal variable in every way (type, scope, address) except one: its value cannot be changed after initialization.

The name sounds contradictory — "constant" + "variable" — but it simply means: a named storage location whose value is fixed for its entire lifetime.

Declaration and initialization

const int MAX_AGE = 60;        /* correct: declare + initialize together */

const int LIMIT;               /* wrong in practice: garbage value locked forever */
LIMIT = 100;                   /* ERROR: assignment of read-only variable */
Golden rule: a constant variable must be given its value at the moment of declaration. If you skip initialization, the variable locks whatever garbage value was in memory, and you can never fix it.

All rules of constant variables

  • Rule 1 — Initialize at declaration: const int x = 10; is the only correct pattern.
  • Rule 2 — No reassignment: any later x = ...; is a compile-time error.
  • Rule 3 — No increment/decrement: x++, x--, x += 5 are all errors too, because they modify x.
  • Rule 4 — Works with every type: int, float, char, arrays, structures and pointers can all be const.
  • Rule 5 — Scope is normal: a const inside a function is local; at file level it is global (with internal linkage differences from C++).
  • Rule 6 — Address is allowed: you can take &x and read through a pointer — you just cannot write.

Common compiler errors with const

const int marks = 95;

marks = 99;        // error: assignment of read-only variable 'marks'
marks++;           // error: increment of read-only variable 'marks'

int *p = &marks;   // warning: discards 'const' qualifier
*p = 99;           // undefined behaviour if forced through cast

The last two lines show a dangerous pattern: pointing a normal pointer at a const variable. The compiler warns you, and if you silence the warning with a cast, modifying the value is undefined behaviour — the program may crash or silently misbehave.

Complete working example

#include <stdio.h>

int main() {
    const int   TOTAL_SEATS = 40;
    const float FEE_PER_SEAT = 1500.50f;

    int booked = 35;
    int vacant = TOTAL_SEATS - booked;

    printf("Total seats : %d\n", TOTAL_SEATS);
    printf("Vacant      : %d\n", vacant);
    printf("Fee/seat    : %.2f\n", FEE_PER_SEAT);
    return 0;
}
Total seats : 40 Vacant : 5 Fee/seat : 1500.50

Constant variable क्या है?

C में constant variable वह variable है जो const qualifier के साथ declare होता है. यह हर तरह से normal variable जैसा behave करता है (type, scope, address) — बस एक फर्क: initialization के बाद इसकी value change नहीं हो सकती.

नाम contradictory लगता है — "constant" + "variable" — लेकिन इसका simple मतलब है: एक named storage location जिसकी value अपनी पूरी lifetime के लिए fixed है.

Declaration और Initialization

const int MAX_AGE = 60;        /* सही: declare + initialize एक साथ */

const int LIMIT;               /* practically गलत: garbage value हमेशा के लिए lock */
LIMIT = 100;                   /* ERROR: assignment of read-only variable */
Golden rule: constant variable को value declaration के समय ही देनी होती है. अगर initialization छोड़ दी, तो memory में जो भी garbage value थी वही lock हो जाती है, और आप उसे कभी ठीक नहीं कर सकते.

Constant variables के सभी rules

  • Rule 1 — Declaration पर initialize करें: const int x = 10; ही एकमात्र correct pattern है.
  • Rule 2 — Reassignment नहीं: बाद में कोई भी x = ...; compile-time error है.
  • Rule 3 — Increment/decrement नहीं: x++, x--, x += 5 — ये सब भी errors हैं, क्योंकि ये x को modify करते हैं.
  • Rule 4 — हर type के साथ काम करता है: int, float, char, arrays, structures और pointers सब const हो सकते हैं.
  • Rule 5 — Scope normal है: function के अंदर const local होता है; file level पर global.
  • Rule 6 — Address लेना allowed है: आप &x ले सकते हैं और pointer से read कर सकते हैं — बस write नहीं कर सकते.

const के common compiler errors

const int marks = 95;

marks = 99;        // error: assignment of read-only variable 'marks'
marks++;           // error: increment of read-only variable 'marks'

int *p = &marks;   // warning: discards 'const' qualifier
*p = 99;           // cast से force करने पर undefined behaviour

आखिरी दो lines एक खतरनाक pattern दिखाती हैं: const variable पर normal pointer point करना. Compiler warning देता है, और अगर cast से warning silence कर दी, तो value modify करना undefined behaviour है — program crash हो सकता है या चुपचाप गलत काम कर सकता है.

पूरा working example

#include <stdio.h>

int main() {
    const int   TOTAL_SEATS = 40;
    const float FEE_PER_SEAT = 1500.50f;

    int booked = 35;
    int vacant = TOTAL_SEATS - booked;

    printf("Total seats : %d\n", TOTAL_SEATS);
    printf("Vacant      : %d\n", vacant);
    printf("Fee/seat    : %.2f\n", FEE_PER_SEAT);
    return 0;
}
Total seats : 40 Vacant : 5 Fee/seat : 1500.50

Frequently Asked Questions

What is a constant variable in C?

A constant variable is a variable declared with the const qualifier whose value is fixed after initialization and cannot be modified anywhere in the program.

Can we take the address of a constant variable?

Yes. A const variable has a memory address, so &x is valid; you can read through a pointer to const but not write.

What happens if a const variable is not initialized?

It locks an unpredictable garbage value permanently, because assignment after declaration is a compile error. Always initialize const at declaration.