*** Welcome to piglix ***

Const (computer programming)


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 constant (does not vary). 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 does not change, 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:


...
Wikipedia

...