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.
size_t count_char(const char *s, char c); /* reads only */ void to_upper(char *s); /* modifies the string */
Try it
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.
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
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
conston by-value parameters is meaningless to callers.void f(const int n)andvoid f(int n)have the same type; theconstonly stops the function body from assigning ton. - Add
constone level deeper for pointers to pointers.char **does not convert implicitly toconst char **orchar *const *as people expect. In C23 and earlier you often needconst 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 inconst char *so the compiler catches writes. Seeconstfor the declaration rules. - Do not cast
constaway 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 returnchar *. constis not a compile-time constant in C.const int n = 5; int a[n];makes a variable-length array, not a fixed one. Useenumorconstexpr(C23) for true constants.
Related
const,restrict- Macros vs. functions
- Tutorial: const and pointers, Functions