In the context of programming languages, a type qualifier is a keyword that can be used to annotate a type to instruct the compiler to treat the now qualified type in a special way.
and C23, there are five type qualifiers in standard C: <code>const</code> (C89), <code>constexpr</code> (C23), <code>volatile</code> (C89), <code>restrict</code> (C99) and <code>_Atomic</code> (C11) â the latter has a private name to avoid clashing with user-defined names. The first two of these, <code>const</code> and <code>volatile</code>, are also present in C++, and are the only type qualifiers in C++. Thus in C++ the term "cv-qualified type" (for const and volatile) is often used for "qualified type", while the terms "c-qualified type" and "v-qualified type" are used when only one of the qualifiers is relevant. C++ has the qualifiers <code>consteval</code> and <code>constinit</code>, not present in C.
Of these, <code>const</code> is by far the best-known and most used, appearing in the C and C++ standard libraries and encountered in any significant use of these languages, which must satisfy const-correctness. The other qualifiers are used for low-level programming, and while widely used there, are rarely used by typical programmers. For a time however <code>volatile</code> was used by some C++ programmers for synchronization during threading, though this was discouraged and is now broken in most compilers.
In D the type constructors are <code>const</code>, <code>immutable</code>, <code>shared</code>, and <code>inout</code>. <code>immutable</code> is a stronger variant of <code>const</code>, indicating data that can never change its value, while <code>const</code> indicates data that cannot be changed through this reference: it is a constant view on possibly mutable data. <code>shared</code> is used for shared data in multi-threading (as <code>volatile</code> was briefly used for in C++). <code>inout</code> is a wildcard used to allow functions that do not modify data (and thus are only concerned with the unqualified type of the data) to return the same qualified type as the input. <code>const</code> and <code>immutable</code> can also be used as storage class specifiers.
In C and C++, a type is given in a function declaration or variable declaration by giving one or more type specifiers, and optionally type qualifiers. For example, an integer variable can be declared as:
where <code>int</code> is the type specifier. An unsigned integer variable can be declared as:
where both <code>unsigned</code> and <code>int</code> are type specifiers. A constant unsigned integer variable can be declared as:
where <code>const</code> is a type qualifier, which the qualified type of <code>x</code> is <code>const unsigned int</code> and the unqualified type is <code>unsigned int</code>.
Variable declarations further have an optional storage class specifier. Properly this is a separate topic, distinct from the type, though <code>const</code> on a variable declaration is also taken to have implications for the storage class, namely that it can be stored in read-only memory.
The other qualifier in C and C++, <code>volatile</code>, indicates that an object may be changed by something external to the program at any time and so must be re-read from memory every time it is accessed.
The qualifier is most often found in code that manipulates hardware directly (such as in embedded systems and device drivers) and in multithreaded applications (though often used incorrectly in that context; see external links at volatile variable). It can be used in exactly the same manner as <code>const</code> in declarations of variables, pointers, references, and member functions, and in fact, <code>volatile</code> is sometimes used to implement a similar design-by-contract strategy which Andrei Alexandrescu calls <code>volatile</code>-correctness, though this is far less common than <code>const</code>-correctness. The <code>volatile</code> qualifier also can be stripped by <code>const_cast</code>, and it can be combined with the <code>const</code> qualifier as in this sample:
Because <code>hardwareRegister</code> is <code>volatile</code>, there is no guarantee that it will hold the same value on two successive reads even though the programmer cannot modify it. The semantics here indicate that the register's value is read-only but not necessarily unchanging.
The notion of a type qualifier was introduced, along with the example of <code>readonly</code> (later renamed <code>const</code>) by Bjarne Stroustrup in a Bell Labs internal Technical Memorandum of 1981, and implemented in C with Classes, the predecessor to C++. As to motivation, Stroustrup writes:
<code>const</code> was then adopted in C as part of standardization, and appears in C89 (and subsequent versions) along with another type qualifier, <code>volatile</code>, which was invented by the ANSI C standard committee (X3J11). <code>volatile</code> appeared by 1985; and an early use was in compiling the UNIX kernel for MIPS, to allow optimized compiling by preventing usual optimizations from being applied to volatile variables. A further qualifier, <code>noalias</code>, was suggested at the December 1987 meeting of the X3J11 committee, but was rejected; its goal was ultimately fulfilled by the <code>restrict</code> qualifier in C99. The motivation for <code>noalias</code> was complementary to <code>volatile</code>, namely that it indicated that even normally unsafe optimizations could be performed. Ritchie was not very supportive of type qualifiers, arguing that they did not "carry their weight", but ultimately did not argue for their removal from the standard; he did oppose <code>noalias</code> however, and it was dropped from the draft.
Java does not have type qualifiers, and conspicuously omitted <code>const</code>: a 1999 proposal to add it was rejected, notably because adding it after the fact and then changing the standard library to use it consistently would have broken compatibility. However, Java initially left open the possibility of implementing <code>const</code>, noticeable in that <code>const</code> is a reserved word, though it is not actually used as a keyword. Instead, Java has the object-oriented keyword <code>final</code>, which is used to qualify attributes (and thence also for local variables) as constant, but not to qualify types.
Other languages take a different approach, considering constancy a property of an identifier (or name binding), not a type. Such languages thus have constant identifiers (corresponding to "variables" that do not vary) with single assignment, but do not have a notion of const-correctness: since constancy is not part of the type, there is no possibility of type mismatch. Examples include Ada 83 with constant objects and a <code>constant</code> keyword, and Java with the <code>final</code> keyword.