Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Keywords and Statements

The const qualifier

Reference for the C const qualifier: read-only variables, pointer to const versus const pointer, const parameters, and why modifying a const object is undefined.

const is a type qualifier that says an object must not be modified through that name. The compiler rejects assignments to it, and it may place the object in read-only memory. Unlike a #define constant, a const object has a type, an address and obeys scope rules.

Where const appears in a pointer declaration changes the meaning. Read the declaration from the name outward: const int *p is "pointer to const int" (the int is read-only), while int *const p is "const pointer to int" (the pointer is fixed).

C
const int max = 100;
const char *name;      /* data read-only, pointer changeable */
char *const buf;       /* pointer fixed, data changeable */

Try it

C Pointer to const vs. const pointer

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 const 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.

Syntax

C
const type name = initializer;
const type *name;          /* pointer to const type */
type *const name;          /* const pointer to type */
Declaration Can change the pointer? Can change the pointed-to data?
int *p yes yes
const int *p (or int const *p) yes no
int *const p no yes
const int *const p no no

Errors / undefined behavior: assigning to a const object through its own name is a compile error. Casting const away and then modifying an object that was defined const is undefined behavior (typically a crash for objects in read-only memory). The following read-only snippet shows it:

C
const int x = 5;
int *p = (int *)&x;
*p = 6;            /* undefined behavior */

Usage notes

  • A const object is not a constant expression in C. const int n = 4; int a[n]; makes a variable length array (see VLAs); use an enum, #define or C23 constexpr when a true constant is required.
  • Must be initialized at definition; at block scope without an initializer its value is indeterminate and cannot be set later.
  • Adding const is implicit (int * converts to const int *); removing it needs a cast and is almost always a bug. Note that char ** does not convert to const char **.
  • String literals have type char[] in C, not const char[], but modifying them is undefined behavior. Declare pointers to them const char *.
  • const parameters document intent and allow the caller to pass read-only data. const on a by-value parameter itself (int f(const int x)) only restricts the function body.
  • Struct members can be const, which makes assigning the whole struct invalid.
  • Optimization: the compiler may assume a const object never changes. Use volatile as well for hardware registers that change on their own.