HOME

TheInfoList



OR:

In the C,
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, and D
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s, a type qualifier is a keyword that is applied to a type, resulting in a ''qualified type.'' For example, const int is a qualified type representing a constant integer, while int is the corresponding unqualified type, simply an integer. In D these are known as
type constructors
'' by analogy with constructors in object-oriented programming. Type qualifiers are a way of expressing additional information about a value through the
type system In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer progra ...
, and ensuring correctness in the use of the data. Type qualifiers are not generally used outside the C/C++ family of languages: many languages have a notation of constants, but express this by the
name binding In programming languages, name binding is the association of entities (data and/or code) with identifiers. An identifier bound to an object is said to reference that object. Machine languages have no built-in notion of identifiers, but name-objec ...
being constant (a "variable that doesn't vary"), rather than through the type system; see
alternatives Founded in 1994, Alternatives, Action and Communication Network for International Development, is a non-governmental, international solidarity organization based in Montreal, Quebec, Canada. Alternatives works to promote justice and equality a ...
, below.


By language


C/C++

and
C11 C11, C.XI, C-11 or C.11 may refer to: Transport * C-11 Fleetster, a 1920s American light transport aircraft for use of the United States Assistant Secretary of War * Fokker C.XI, a 1935 Dutch reconnaissance seaplane * LET C-11, a license-build var ...
, there are four type qualifiers in standard C:
const In some 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, in the C family of languages differs from similar constructs i ...
( C89), volatile ( C89),
restrict In the C programming language, restrict is a keyword, introduced by the C99 standard, that can be used in pointer declarations. By adding this type qualifier, a programmer hints to the compiler that for the lifetime of the pointer, no other p ...
(
C99 C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps impl ...
) and _Atomic (
C11 C11, C.XI, C-11 or C.11 may refer to: Transport * C-11 Fleetster, a 1920s American light transport aircraft for use of the United States Assistant Secretary of War * Fokker C.XI, a 1935 Dutch reconnaissance seaplane * LET C-11, a license-build var ...
) – the latter has a private name to avoid clashing with user-defined names. The first two of these, const and volatile, 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. Of these, const 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 In some 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, in the C family of languages differs from similar constructs in ...
. The other qualifiers are used for low-level programming, and while widely used there, are rarely used by typical programmers. For a time however volatile was used by some C++ programmers for synchronization during threading, though this was discouraged and is now broken in most compilers.


D

In D the type constructors are const, immutable, shared, and inout. immutable is a stronger variant of const, indicating data that can ''never'' change its value, while const indicates data that cannot be changed through this reference: it is a constant ''view'' on possibly mutable data. shared is used for shared data in multi-threading (as volatile was briefly used for in C++). inout 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. const and immutable can also be used as storage class specifiers.


Syntax

In C and C++, a type is given in a function
declaration Declaration may refer to: Arts, entertainment, and media Literature * ''Declaration'' (book), a self-published electronic pamphlet by Michael Hardt and Antonio Negri * ''The Declaration'' (novel), a 2008 children's novel by Gemma Malley Music ...
or variable declaration by giving one or more type specifiers, and optionally type qualifiers. For example, an integer variable can be declared as: int x; where int is the type specifier. An unsigned integer variable can be declared as: unsigned int x; where both unsigned and int are type specifiers. A constant unsigned integer variable can be declared as: const unsigned int x; where const is a type qualifier, which the qualified type of x is const unsigned int and the unqualified type is unsigned int. Variable declarations further have an optional
storage class specifier Storage may refer to: Goods Containers * Dry cask storage, for storing high-level radioactive waste * Food storage * Intermodal container, cargo shipping * Storage tank Facilities * Garage (residential), a storage space normally used to store car ...
. Properly this is a separate topic, distinct from the type, though const on a variable declaration is ''also'' taken to have implications for the storage class, namely that it can be stored in read-only memory.


Volatile-correctness

The other qualifier in C and C++, volatile, 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 system An embedded system is a computer system—a combination of a computer processor, computer memory, and input/output peripheral devices—that has a dedicated function within a larger mechanical or electronic system. It is ''embedded'' as ...
s and
device driver In computing, a device driver is a computer program that operates or controls a particular type of device that is attached to a computer or automaton. A driver provides a software interface to hardware devices, enabling operating systems and ot ...
s) and in multithreaded applications (though often used incorrectly in that context; see external links at
volatile variable In computer programming, particularly in the C, C++, C#, and Java programming languages, the volatile keyword indicates that a value may change between different accesses, even if it does not appear to be modified. This keyword prevents an opti ...
). It can be used in exactly the same manner as const in declarations of variables, pointers, references, and member functions, and in fact, volatile is sometimes used to implement a similar design-by-contract strategy which
Andrei Alexandrescu Andrei Alexandrescu (born 1969) is a Romanian-American C++ and D language programmer and author. He is particularly known for his pioneering work on policy-based design implemented via template metaprogramming. These ideas are articulated ...
calls volatile-correctness, though this is far less common than const-correctness. The volatile qualifier also can be stripped by const_cast, and it can be combined with the const qualifier as in this sample: // Set up a reference to a read-only hardware register that is // mapped in a hard-coded memory location. const volatile int & hardwareRegister = *reinterpret_cast(0x8000); int currentValue = hardwareRegister; // Read the memory location int newValue = hardwareRegister; // Read it again hardwareRegister = 5; // Error, cannot write to a const location Because hardwareRegister is volatile, 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.


History

The notion of a type qualifier was introduced, along with the example of readonly (later renamed const) by
Bjarne Stroustrup Bjarne Stroustrup (; ; born 30 December 1950) is a Danish computer scientist, most notable for the invention and development of the C++ programming language. As of July 2022, Stroustrup is a professor of Computer Science at Columbia University. ...
in a Bell Labs internal Technical Memorandum of 1981, and implemented in
C with Classes C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, the predecessor to
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
.Sibling Rivalry: C and C++
Bjarne Stroustrup Bjarne Stroustrup (; ; born 30 December 1950) is a Danish computer scientist, most notable for the invention and development of the C++ programming language. As of July 2022, Stroustrup is a professor of Computer Science at Columbia University. ...
, 2002, p. 5
As to motivation, Stroustrup writes: :"It served two functions: as a way of defining a symbolic constant that obeys scope and type rules (that is, without using a macro) and as a way of deeming an object in memory immutable." const was then adopted in C as part of standardization, and appears in C89 (and subsequent versions) along with another type qualifier, volatile, which was invented by the ANSI C standard committee (X3J11). volatile 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, noalias, was suggested at the December 1987 meeting of the X3J11 committee, but was rejected; its goal was ultimately fulfilled by the restrict qualifier in C99. The motivation for noalias was complementary to volatile, 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 noalias however, and it was dropped from the draft. Java does not have type qualifiers, and conspicuously omitted const: 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 const, noticeable in that const is a
reserved word In a computer language, a reserved word (also known as a reserved identifier) is a word that cannot be used as an identifier, such as the name of a variable, function, or label – it is "reserved from use". This is a syntactic definition, and a re ...
, though it is not actually used as a
keyword Keyword may refer to: Computing * Keyword (Internet search), a word or phrase typically used by bloggers or online content creator to rank a web page on a particular topic * Index term, a term used as a keyword to documents in an information syst ...
. Instead, Java has the object-oriented keyword final, which is used to qualify attributes (and thence also for local variables) as constant, but not to qualify types.


Alternatives

Other languages take a different approach, considering constancy a property of an ''identifier'' (or
name binding In programming languages, name binding is the association of entities (data and/or code) with identifiers. An identifier bound to an object is said to reference that object. Machine languages have no built-in notion of identifiers, but name-objec ...
), 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 Ada is a structured, statically typed, imperative, and object-oriented high-level programming language, extended from Pascal and other languages. It has built-in language support for '' design by contract'' (DbC), extremely strong typing, exp ...
with constant objects and a constant keyword,1815A

"The declared object is a constant if the reserved word constant appears in the object declaration; the declaration must then include an explicit initialization. The value of a constant cannot be modified after initialization. Formal parameters of mode in of subprograms and entries, and generic formal parameters of mode in, are also constants; a loop parameter is a constant within the corresponding loop; a subcomponent or slice of a constant is a constant."
and Java with the final keyword.


Notes


References

{{Reflist, 30em


External links


C11 standard


C (programming language) C++