Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Functions and Scope

const correctness

Learn const correctness in C: mark read-only parameters, return const pointers, avoid casting const away and see how const propagates through call chains.

Const correctness means using const everywhere a value is not supposed to change, so the compiler can check your promises. A function that only reads through a pointer should take a pointer to const; a function that returns a view into internal data should return a pointer to const. When every layer does this, accidental writes become compile-time diagnostics instead of runtime bugs.

The rule of thumb is simple: start with const on every pointer parameter and remove it only when the function really writes through it. Once one function in a call chain drops const unnecessarily, every caller holding read-only data must either cast or copy.

C
size_t count_char(const char *s, char c);   /* reads only */
void   to_upper(char *s);                   /* modifies the string */

Try it

C Read-only and writable parameters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Returning a const view

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
return_type f(const T *p);        /* *p is read-only through p */
return_type f(T *const p);        /* p itself is fixed (rarely useful for parameters) */
const T *g(void);                 /* returns a pointer to read-only data */
Name Description
const T *p Pointer to const: the pointed-to object cannot be changed through p.
T *const p Const pointer: p cannot be reassigned, but *p can change.
const T *const p Neither can change.

Errors / undefined behavior: modifying an object that was defined const (even through a cast) is undefined behavior. Discarding const with an implicit conversion is a constraint-style diagnostic: GCC warns by default.

Usage notes

  • Top-level const on by-value parameters is meaningless to callers. void f(const int n) and void f(int n) have the same type; the const only stops the function body from assigning to n.
  • Add const one level deeper for pointers to pointers. char ** does not convert implicitly to const char ** or char *const * as people expect. In C23 and earlier you often need const char *const * in the parameter and a cast or a copy at the call site.
  • String literals have type char[], but writing to them is undefined behavior. Store them in const char * so the compiler catches writes. See const for the declaration rules.
  • Do not cast const away to call a badly declared function. If the function truly never writes, fix its signature; if it writes, copy the data first.
  • Standard functions model this well. strlen(const char *) reads, strcpy(char *, const char *) writes the first argument and reads the second. A few return a non-const pointer into a const argument (strchr, strstr); C23 makes those functions type-generic on constness in a conforming implementation, but older libraries still return char *.
  • const is not a compile-time constant in C. const int n = 5; int a[n]; makes a variable-length array, not a fixed one. Use enum or constexpr (C23) for true constants.