Const (computer programming)


In some programming languages, is a type qualifier that indicates that the data is read-only. While this can be used to declare constants, in the C family of languages differs from similar constructs in other languages in that it is part of the type, and thus has complicated behavior when combined with pointers, references, composite data types, and type-checking. In other languages, the data is not in a single memory location, but copied at compile time for each use. Languages which use it include C, C++, D, JavaScript, Julia, and Rust.

Introduction

When applied in an object declaration, it indicates that the object is a constant: its value may not be changed, unlike a variable. This basic use – to declare constants – has parallels in many other languages.
However, unlike in other languages, in the C family of languages the const is part of the type, not part of the object. For example, in C, declares an object x of int const type – the const is part of the type, as if it were parsed " x" – while in Ada, declares a constant X of INTEGER type: the constant is part of the object, but not part of the type.
This has two subtle results. Firstly, const can be applied to parts of a more complex type – for example, int const* const x; declares a constant pointer to a constant integer, while int const* x; declares a variable pointer to a constant integer, and int* const x; declares a constant pointer to a variable integer. Secondly, because const is part of the type, it must match as part of type-checking. For example, the following code is invalid:

void f;
//...
int const i;
f;

because the argument to f must be a variable integer, but i is a constant integer. This matching is a form of program correctness, and is known as const-correctness. This allows a form of programming by contract, where functions specify as part of their type signature whether they modify their arguments or not, and whether their return value is modifiable or not. This type-checking is primarily of interest in pointers and references – not basic value types like integers – but also for composite data types or templated types such as containers. It is concealed by the fact that the const can often be omitted, due to type coercion and C being call-by-value.

Consequences

The idea of const-ness does not imply that the variable as it is stored in computer memory is unwritable. Rather, const-ness is a compile-time construct that indicates what a programmer should do, not necessarily what they can do. Note, however, that in the case of predefined data, C const is often unwritable.

Distinction from constants

While a constant does not change its value while the program is running, an object declared const may indeed change its value while the program is running. A common example are read only registers within embedded systems like the current state of a digital input. The data registers for digital inputs are often declared as const and volatile. The content of these registers may change without the program doing anything but it would be ill-formed for the program to attempt write to them.

Other uses

In addition, a member-function can be declared as const. In this case, the this pointer inside such a function is of type ObjectType const* rather than merely of type ObjectType*. This means that non-const functions for this object cannot be called from inside such a function, nor can member variables be modified. In C++, a member variable can be declared as mutable, indicating that this restriction does not apply to it. In some cases, this can be useful, for example with caching, reference counting, and data synchronization. In these cases, the logical meaning of the object is unchanged, but the object is not physically constant since its bitwise representation may change.

Syntax

In C, C++, and D, all data types, including those defined by the user, can be declared const, and const-correctness dictates that all variables or objects should be declared as such unless they need to be modified. Such proactive use of const makes values "easier to understand, track, and reason about", and it thus increases the readability and comprehensibility of code and makes working in teams and maintaining code simpler because it communicates information about a value's intended use. This can help the compiler as well as the developer when reasoning about code. It can also enable an optimizing compiler to generate more efficient code.

Simple data types

For simple non-pointer data types, applying the const qualifier is straightforward. It can go on either side of some types for historical reasons. On some implementations, using const twice generates a warning but not an error.

Pointers and references

For pointer and reference types, the meaning of const is more complicated – either the pointer itself, or the value being pointed to, or both, can be const. Further, the syntax can be confusing. A pointer can be declared as a const pointer to writable value, or a writable pointer to a const value, or const pointer to const value. A const pointer cannot be reassigned to point to a different object from the one it is initially assigned, but it can be used to modify the value that it points to. Reference variables in C++ are an alternate syntax for const pointers. A pointer to a const object, on the other hand, can be reassigned to point to another memory location, but it cannot be used to modify the memory that it is pointing to. A const pointer to a const object can also be declared and can neither be used to modify the pointee nor be reassigned to point to another object. The following code illustrates these subtleties:

void foo

C convention

Following usual C convention for declarations, declaration follows use, and the * in a pointer is written on the pointer, indicating dereferencing. For example, in the declaration int *ptr, the dereferenced form *ptr is an int, while the reference form ptr is a pointer to an int. Thus const modifies the name to its right. The C++ convention is instead to associate the * with the type, as in int* ptr, and read the const as modifying the type to the left. int const * ptrToConst can thus be read as "*ptrToConst is a int const", or "ptrToConst is a int const *". Thus:

int* ptr; // *ptr is an int value
int const* ptrToConst; // *ptrToConst is a constant int
int* const constPtr; // constPtr is a constant int*
int const* const constPtrToConst; // constPtrToConst is a constant pointer and points to a constant value

C++ convention

Following C++ convention of analyzing the type, not the value, a rule of thumb is to read the declaration from right to left. Thus, everything to the left of the star can be identified as the pointed type and everything to the right of the star are the pointer properties. For instance, in our example above, int const* can be read as a writable pointer that refers to a non-writable integer, and int* const can be read as a non-writable pointer that refers to a writable integer.
A more generic rule to aid in understanding complex declarations and definitions works like this:
  1. find the identifier of the declaration in question
  2. read as far as possible to the right
  3. back up to where reading began, and read backwards to the left
  4. once reaching the beginning of the declaration, finish. If not, continue at step 2, beyond the closing parenthesis that was matched last.
Here is an example:
When reading to the left, it is important to read the elements from right to left. So an int const* becomes a pointer to a const int and not a const pointer to an int.
In some cases C/C++ allows the const keyword to be placed to the left of the type. Here are some examples:

const int* ptrToConst; //identical to: int const* ptrToConst,
const int* const constPtrToConst; //identical to: int const* const constPtrToConst

Although C/C++ allows such definitions, the compiler still reads the definitions according to the abovementioned procedure: from right to left. But putting const before what must be constant quickly introduces mismatches between what is intended to be written and what the compiler decides was written. Consider pointers to pointers:

int** ptr; // a pointer to a pointer to ints
int const** ptr; // a pointer to a pointer to constant int value
int* const* ptr; // a pointer to a const pointer to int values
int** const ptr; // a constant pointer to pointers to ints
int const** const ptr; // a constant pointer to pointers to constant int values

Some choose to write the pointer symbol on the variable, reasoning that attaching it to the type is potentially confusing, as it strongly suggests a pointer "type" which is not necessarily the case in C.

// two ways:
int* a; // left-aligned asterisk
int *a; // right-aligned asterisk
int* a, b; // confusing
int *a, b; // a is a pointer to an int and b is an int
int* a, *b; // ugly
int *a, *b;

Note that in C#, right-alignment of the asterisk is illegal.

int *a, *b; // illegal in C#
int* a, b; // declares two int*

Bjarne Stroustrup's FAQ recommends only declaring one variable per line if using the C++ convention, to avoid this issue.
The same considerations apply to defining references and rvalue references:

int myInt = 22;
int const& refToConst = myInt;
int const& ref2 = myInt, ref3 = myInt;
// confusing:
// ref2 is a reference, but ref3 isn't:
// ref3 is a constant int initialized with myInt's value
// error: as references can't change anyway.
int &const constRef = myInt;
// C++:
int&& rref = int, value = 10; // confusing: rref is an rvalue reference, but value is a mere int.
int &&rref = int, value = 10;

More complicated declarations are encountered when using multidimensional arrays and references to pointers. Although it is sometimes argued that such declarations are confusing and error-prone and that they therefore should be avoided or be replaced by higher-level structures, the procedure described at the top of this section can always be used without introducing ambiguities or confusion.