language-icon Old Web
English
Sign In

const

In the C, C++, D, and JavaScript programming languages, const is a type qualifier: a keyword applied to a data type that indicates that the data is read only. While this can be used to declare constants, const in the C family of languages differs from similar constructs in other languages in being part of the type, and thus has complicated behavior when combined with pointers, references, composite data types, and type-checking. In the C, C++, D, and JavaScript programming languages, const is a type qualifier: a keyword applied to a data type that indicates that the data is read only. While this can be used to declare constants, const in the C family of languages differs from similar constructs in other languages in being part of the type, and thus has complicated behavior when combined with pointers, references, composite data types, and type-checking. 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, const int x = 1; declares an object x of const int type – the const is part of the type, as if it were parsed “(const int) x” – while in Ada, X : constant INTEGER := 1_ declares a constant (a kind of object) 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: 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 (implicit type conversion) and C being call-by-value (C++ and D are either call-by-value or call-by-reference). 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 (such as const char * string literals), C const is often unwritable. 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 (volatile) but you shall not write to them either (const). In addition, a (non-static) member-function can be declared as const. In this case, the this pointer inside such a function is of type object_type const * const rather than merely of type object_type * const. 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 (state) of the object is unchanged, but the object is not physically constant since its bitwise representation may change. 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.

[ "Mathematical analysis", "Programming language", "Humanities", "C++ Standard Library", "C mathematical functions", "Compatibility of C and C++", "C++/CX", "Sequence container" ]
Parent Topic
Child Topic
    No Parent Topic