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

What is const in C? Meaning, Syntax and Simple Examples

const in C is a keyword that makes a variable read-only after initialization. Learn its exact meaning, syntax, rules and beginner-friendly examples.

What is const in C?

Definition: const is a type qualifier in C that makes a variable read-only. Once a const variable is initialized, its value cannot be changed anywhere in the program. If you try to modify it, the compiler gives an error.

Think of const as a lock. A normal variable is an open box — you can change its contents any time. A const variable is a locked box — you put the value in once (at declaration) and the compiler refuses every attempt to change it later.

Syntax of const

const data_type variable_name = value;

Examples:

const int MAX_MARKS = 100;
const float PI = 3.14159f;
const char GRADE = 'A';

You can also write int const MAX_MARKS = 100; — both orders mean exactly the same thing.

Simple Example

#include <stdio.h>

int main() {
    const int PASS_MARKS = 33;
    printf("Passing marks: %d\n", PASS_MARKS);

    // PASS_MARKS = 40;   // ERROR: assignment of read-only variable
    return 0;
}
Passing marks: 33

If you remove the comment from PASS_MARKS = 40;, the compiler stops with an error like "assignment of read-only variable 'PASS_MARKS'". This is the whole purpose of const — the compiler protects the value for you.

Why do we use const?

  • Safety: Values like tax rate, PI, or max size should never change accidentally. const makes accidental change impossible.
  • Readability: const int MAX_STUDENTS = 60; tells the reader this value is fixed by design.
  • Compiler optimization: The compiler can optimize code better when it knows a value never changes.
  • Safe function parameters: void print(const char *msg) promises the function will not modify the string you pass.

Important Rules of const

  • A const variable must be initialized at declaration: const int x; followed by x = 5; is an error.
  • const does not make the value a compile-time constant in C — you still cannot use a const int as a case label in switch (use #define or enum for that).
  • const applies to the variable, not the data type — you can have many const and non-const ints in the same program.

C में const क्या है?

Definition: const C का एक type qualifier है जो variable को read-only बना देता है. एक बार const variable initialize हो गया, तो पूरे program में उसकी value change नहीं की जा सकती. Change करने की कोशिश करने पर compiler error देता है.

const को एक ताले (lock) की तरह समझें. Normal variable एक खुला box है — आप कभी भी उसकी value बदल सकते हैं. const variable एक locked box है — value एक बार (declaration पर) डालते हैं और उसके बाद compiler हर बदलाव की कोशिश को रोक देता है.

const का Syntax

const data_type variable_name = value;

Examples:

const int MAX_MARKS = 100;
const float PI = 3.14159f;
const char GRADE = 'A';

int const MAX_MARKS = 100; लिखना भी बिल्कुल same है — दोनों orders का मतलब एक ही है.

आसान Example

#include <stdio.h>

int main() {
    const int PASS_MARKS = 33;
    printf("Passing marks: %d\n", PASS_MARKS);

    // PASS_MARKS = 40;   // ERROR: assignment of read-only variable
    return 0;
}
Passing marks: 33

अगर आप PASS_MARKS = 40; से comment हटा दें, तो compiler error देगा — "assignment of read-only variable 'PASS_MARKS'". यही const का पूरा purpose है — compiler आपकी value को protect करता है.

const का उपयोग क्यों करते हैं?

  • Safety: Tax rate, PI, या max size जैसी values कभी गलती से change नहीं होनी चाहिए. const accidental change को impossible बना देता है.
  • Readability: const int MAX_STUDENTS = 60; पढ़ने वाले को बताता है कि यह value design से fixed है.
  • Compiler optimization: जब compiler को पता हो कि value कभी नहीं बदलेगी, तो वह code बेहतर optimize कर सकता है.
  • Safe function parameters: void print(const char *msg) promise करता है कि function आपकी string को modify नहीं करेगा.

const के ज़रूरी Rules

  • const variable को declaration पर ही initialize करना ज़रूरी है: पहले const int x; फिर x = 5; — यह error है.
  • C में const value को compile-time constant नहीं बनाता — const int को switch के case label में use नहीं कर सकते (उसके लिए #define या enum use करें).
  • const variable पर apply होता है, data type पर नहीं — एक ही program में const और non-const दोनों ints हो सकते हैं.

Frequently Asked Questions

What is const in C in one line?

const is a type qualifier that makes a variable read-only, so its value cannot be changed after initialization.

Can we declare const without initializing it?

No. A const variable must be initialized at the time of declaration, because you cannot assign to it later.

Is const the same as #define?

No. const creates a typed, scoped read-only variable checked by the compiler, while #define is a simple text replacement done by the preprocessor.