On this page
const
Prev: __STDC_VERSION__ | Up: builtins | Next: static
Rel: Pointers & Pointer Arithmetic
Ref: cppreference — const
Tags: #public #prog #c #builtins #tier/core
Availability
Built-in (Type Qualifier) — requires no #include header.
Syntax & Overview
const T x; | const T *p;
Marks an object or pointee as read-only through that access path.
Notes & Pitfalls
- Read pointer declarations right-to-left:
const char *p= pointer to const char;char *const p= const pointer to mutable char. - Modifying an originally
constobject via a cast is UB.
Example
#include <assert.h>
#include <string.h>
static size_t measure(const char *s) { return strlen(s); }
int main(void) { assert(measure("immutable") == 9); }
Linked from
Related notes
- tuts/c/builtins/__STDC_VERSION__ Linked here; links to this page; shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/static Linked here; links to this page; shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins Linked here; links to this page; shared tags: #builtins, #c, #prog
- tuts/c/pointers_and_memory/pointers_and_pointer_arithmetic Linked here; shared tags: #c, #prog
- tuts/c/builtins/__FILE__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__func__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__LINE__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/_Alignof shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/_Atomic shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/_Generic shared tags: #builtins, #c, #prog, #tier