C const Qualifier

Advanced
6 min read
Updated: Nov 2026
1 example

What Is const Qualifier?

const tells the compiler an object must not be modified through that name. Use it for constants, read-only function parameters (const char *, const int *), and returned strings. Const correctness catches accidental writes at compile time and lets the compiler optimize.

Remember
const int LIMIT = 3;              /* read-only object   */
int sum(const int *a, int n);       /* function won't modify */
const char *name = "Ada";           /* pointer to const char */

Cheat Sheet

Keep these facts handy while you work with const Qualifier in C.

TopicDetail
Variableconst T x = ...;
Pointer to constconst T *p
Const pointerT *const p
Cast awayAvoid; modifying a const object is UB

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: volatile Qualifier.

Example 1 — A read-only array parameter

The compiler rejects writes through a.

C
#include <stdio.h>

static const int LIMIT = 3;

static int sum(const int *a, int n) {
    int total = 0;
    for (int i = 0; i < n; i++) {
        total += a[i];       /* a[i] = 0; would not compile */
    }
    return total;
}

int main(void) {
    int v[] = {1, 2, 3};
    printf("sum=%d (limit %d)\n", sum(v, LIMIT), LIMIT);
    return 0;
}

How It Works

  1. const int *a promises that sum will not change the array.
  2. LIMIT is a read-only object.
  3. Callers can pass either const or non-const arrays safely.

Edge Cases & Pitfalls

Check these before relying on const Qualifier in real programs.

cast

Casting const away

Writing to an object defined const through a cast pointer is undefined behavior.

literal

String literals

Use const char * for string literals so accidental writes fail to compile.

Key Takeaways

  • Document: input parameters are const.
  • Catch: writes fail at compile time.
  • Honest: do not cast it away.

One line: const tells the compiler an object must not be modified through that name.

Frequently Asked Questions

Declaring everything that should not change as const, especially pointer parameters, so the compiler enforces read-only access.
Not in C. A const int cannot be used for array sizes at file scope or in case labels; use enum or #define.
Continue with "C volatile Qualifier" next. Stop the compiler from caching a variable that can change unexpectedly. The C Advanced Concepts hub lists every topic in order.

Next: volatile Qualifier

Stop the compiler from caching a variable that can change unexpectedly.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful