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

const Keyword in C with Example: 7 Practical Programs

7 practical, compilable examples of the const keyword in C: const variables, const with functions, const arrays, const parameters and more.

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

Where is the const keyword most used in real programs?

The most common professional use is const function parameters like void print(const char *s), which promise the function will not modify the passed data.

Can a const array element be changed?

No. When an array is declared const, all its elements become read-only and any modification attempt is a compiler error.

What error comes if we modify a const variable?

GCC shows: error: assignment of read-only variable, which means the compiler blocked the write to a const object.