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

C में const Keyword Example के साथ: 7 Practical Programs

C में const keyword के 7 practical examples: const variables, const with functions, const arrays, const parameters और बहुत कुछ.

const keyword: quick recap

The const keyword makes a variable read-only. Below are 7 practical, compilable examples showing every common place const is used in real C programs.

Example 1: Basic const variable

#include <stdio.h>
int main() {
    const int MAX_MARKS = 100;
    printf("Maximum marks: %d\n", MAX_MARKS);
    return 0;
}
Maximum marks: 100

Example 2: Compiler error on modification

int main() {
    const int LIMIT = 50;
    LIMIT = 60;      // ERROR: assignment of read-only variable 'LIMIT'
    return 0;
}

This program does not compile. The error itself is the feature — const catches the bug before the program ever runs.

Example 3: const in a calculation

#include <stdio.h>
int main() {
    const float PI = 3.14159f;
    float r = 5.0f;
    printf("Circumference = %.2f\n", 2 * PI * r);
    return 0;
}
Circumference = 31.42

Example 4: const function parameter

#include <stdio.h>

void printName(const char *name) {
    // name[0] = 'X';   // ERROR: cannot modify through const pointer
    printf("Student: %s\n", name);
}

int main() {
    printName("Gagan");
    return 0;
}
Student: Gagan

This is the most important professional use of const: the function promises not to modify the caller's data. Every standard library function like strlen(const char *s) uses this pattern.

Example 5: const array

#include <stdio.h>
int main() {
    const int weekdays[7] = {1, 2, 3, 4, 5, 6, 7};
    // weekdays[0] = 99;   // ERROR: array elements are read-only
    printf("First day code: %d\n", weekdays[0]);
    return 0;
}
First day code: 1

Example 6: const with a pointer

#include <stdio.h>
int main() {
    int marks = 85;
    const int *p = &marks;   // pointer to const int
    printf("Marks: %d\n", *p);
    // *p = 90;              // ERROR: cannot change value through p
    marks = 90;              // OK: variable itself is not const
    printf("Marks: %d\n", *p);
    return 0;
}
Marks: 85 Marks: 90

Example 7: const in a real mini program

#include <stdio.h>

int main() {
    const float GST_RATE = 0.18f;      /* fixed by law   */
    const int   FREE_LIMIT = 500;      /* business rule  */
    float bill = 1200.0f;

    float gst = (bill > FREE_LIMIT) ? bill * GST_RATE : 0;
    printf("Bill: %.2f, GST: %.2f, Total: %.2f\n", bill, gst, bill + gst);
    return 0;
}
Bill: 1200.00, GST: 216.00, Total: 1416.00

Business rules like tax rates are perfect const candidates — if a teammate accidentally writes GST_RATE = 0; somewhere, the compiler catches it instantly.

const keyword: quick recap

const keyword variable को read-only बना देता है. नीचे 7 practical, compilable examples हैं जो real C programs में const की हर common जगह दिखाते हैं.

Example 1: Basic const variable

#include <stdio.h>
int main() {
    const int MAX_MARKS = 100;
    printf("Maximum marks: %d\n", MAX_MARKS);
    return 0;
}
Maximum marks: 100

Example 2: Modify करने पर compiler error

int main() {
    const int LIMIT = 50;
    LIMIT = 60;      // ERROR: assignment of read-only variable 'LIMIT'
    return 0;
}

यह program compile नहीं होता. यही error असली feature है — const bug को program चलने से पहले ही पकड़ लेता है.

Example 3: Calculation में const

#include <stdio.h>
int main() {
    const float PI = 3.14159f;
    float r = 5.0f;
    printf("Circumference = %.2f\n", 2 * PI * r);
    return 0;
}
Circumference = 31.42

Example 4: const function parameter

#include <stdio.h>

void printName(const char *name) {
    // name[0] = 'X';   // ERROR: const pointer से modify नहीं कर सकते
    printf("Student: %s\n", name);
}

int main() {
    printName("Gagan");
    return 0;
}
Student: Gagan

यह const का सबसे important professional use है: function promise करता है कि caller का data modify नहीं करेगा. strlen(const char *s) जैसा हर standard library function यही pattern use करता है.

Example 5: const array

#include <stdio.h>
int main() {
    const int weekdays[7] = {1, 2, 3, 4, 5, 6, 7};
    // weekdays[0] = 99;   // ERROR: array elements read-only हैं
    printf("First day code: %d\n", weekdays[0]);
    return 0;
}
First day code: 1

Example 6: Pointer के साथ const

#include <stdio.h>
int main() {
    int marks = 85;
    const int *p = &marks;   // pointer to const int
    printf("Marks: %d\n", *p);
    // *p = 90;              // ERROR: p के through value change नहीं कर सकते
    marks = 90;              // OK: variable खुद const नहीं है
    printf("Marks: %d\n", *p);
    return 0;
}
Marks: 85 Marks: 90

Example 7: Real mini program में const

#include <stdio.h>

int main() {
    const float GST_RATE = 0.18f;      /* कानून से fixed  */
    const int   FREE_LIMIT = 500;      /* business rule  */
    float bill = 1200.0f;

    float gst = (bill > FREE_LIMIT) ? bill * GST_RATE : 0;
    printf("Bill: %.2f, GST: %.2f, Total: %.2f\n", bill, gst, bill + gst);
    return 0;
}
Bill: 1200.00, GST: 216.00, Total: 1416.00

Tax rates जैसे business rules perfect const candidates हैं — अगर कोई teammate गलती से कहीं GST_RATE = 0; लिख दे, तो compiler तुरंत पकड़ लेता है.

Frequently Asked Questions

Real programs में const keyword सबसे ज़्यादा कहां use होता है?

सबसे common professional use const function parameters है, जैसे void print(const char *s), जो promise करता है कि function passed data को modify नहीं करेगा.

क्या const array का element change हो सकता है?

नहीं. जब array const declare होता है, तो उसके सभी elements read-only हो जाते हैं और modify करने पर compiler error आता है.

const variable modify करने पर कौन-सा error आता है?

GCC दिखाता है: error: assignment of read-only variable, यानी compiler ने const object पर write को रोक दिया.