Const (computer programming)

Last updated

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, const 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. [1] Languages which use it include C, C++, D, JavaScript, Julia, and Rust.

Contents

Introduction

When applied in an object declaration, [lower-alpha 1] 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, intconstx=1; declares an object x of int const type – the const is part of the type, as if it were parsed "(int const) x" – while in Ada, X:constantINTEGER:=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:

voidf(int&x);// ...intconsti;f(i);

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).

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 (such as char const * string literals), 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 (volatile) but it would be ill-formed for the program to attempt write to them (const).

Other uses

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 * rather than merely of type object_type *. [2] 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.

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", [3] 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. [4]

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 (for example, const char foo = 'a'; is equivalent to char const foo = 'a';). On some implementations, using const twice (for instance, const char const or char const const) 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 (called the pointee ). [5] [6] [7] [8] [9] 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 (which should be an object of the same type or of a convertible type), 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 apointee nor be reassigned to point to another object. The following code illustrates these subtleties:

voidFoo(int*ptr,intconst*ptrToConst,int*constconstPtr,intconst*constconstPtrToConst){*ptr=0;// OK: modifies the pointed to dataptr=NULL;// OK: modifies the pointer*ptrToConst=0;// Error! Cannot modify the pointed to dataptrToConst=NULL;// OK: modifies the pointer*constPtr=0;// OK: modifies the pointed to dataconstPtr=NULL;// Error! Cannot modify the pointer*constPtrToConst=0;// Error! Cannot modify the pointed to dataconstPtrToConst=NULL;// Error! Cannot modify the pointer}

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" (the value is constant), or "ptrToConst is a int const *" (the pointer is a pointer to a constant integer). Thus:

int*ptr;// *ptr is an int valueintconst*ptrToConst;// *ptrToConst is a constant (int: integer value)int*constconstPtr;// constPtr is a constant (int *: integer pointer)intconst*constconstPtrToConst;// 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 that helps you understand complex declarations and definitions works like this:

  1. find the identifier whose declaration you want to understand
  2. read as far as possible to the right (i.e., until the end of the declaration or to the next closing parenthesis, whichever comes first)
  3. back up to where you began, and read backwards to the left (i.e., until the beginning of the declaration or to the open-parenthesis matching the closing parenthesis found in the previous step)
  4. when you've reached the beginning of the declaration you're done. If not, continue at step 2, beyond the closing parenthesis that was matched last.

Here is an example:

Part of expression
double(**const(*fun(int))(double))[10]
Meaning
(reading downwards)
Identifier
fun
fun is a ...
Read to the right
(int))
function expecting an int ...
Find the matching (
(*
returning a pointer to ...
Continue right
(double))
a function expecting a double ...
Find the matching (
(**const
returning a constant pointer to
a pointer to ...
Continue right
[10]
blocks of 10 ...
Read to the left
double
doubles.

When reading to the left, it is important that you 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:

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

Although C/C++ allows such definitions (which closely match the English language when reading the definitions from left to right), the compiler still reads the definitions according to the abovementioned procedure: from right to left. But putting constbefore what must be constant quickly introduces mismatches between what you intend to write and what the compiler decides you wrote. Consider pointers to pointers:

int**ptr;// a pointer to a pointer to intsintconst**ptr// a pointer to a pointer to constant int value// (not a pointer to a constant pointer to ints)int*const*ptr// a pointer to a const pointer to int values// (not a constant pointer to a pointer to ints)int**constptr// a constant pointer to pointers to ints// (ptr, the identifier, being const makes no sense)intconst**constptr// a constant pointer to pointers to constant int values

As a final note regarding pointer definitions: always write the pointer symbol (the *) as much as possible to the right. Attaching the pointer symbol to the type is tricky, as it strongly suggests a pointer type, which isn't the case. Here are some examples:

int*a;/* write: */int*a;// a is a pointer to an intint*a,b;// CONFUSING /* write: */int*a,b;// a is a pointer to an int, //                             but b is a mere intint*a,*b;// UGLY: both a and b are pointers to ints/* write: */int*a,*b;

Bjarne Stroustrup's FAQ recommends only declaring one variable per line if using the C++ convention, to avoid this issue. [10]

The same considerations apply to defining references and rvalue references:

intvar=22;intconst&refToConst=var;// OKintconst&ref2=var,ref3=var;// CONFUSING:// ref2 is a reference, but ref3 isn't:// ref3 is a constant int initialized with// var's valueint&constconstRef=var;// ERROR: as references can't change anyway.// C++:int&&rref=int(5),value=10;// CONFUSING:// rref is an rvalue reference, but value is// a mere int. /* write: */int&&rref=int(5),value=10;

More complicated declarations are encountered when using multidimensional arrays and references (or pointers) to pointers. Although it is sometimes argued [ who? ] 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.

Parameters and variables

const can be declared both on function parameters and on variables (static or automatic, including global or local). The interpretation varies between uses. A const static variable (global variable or static local variable) is a constant, and may be used for data like mathematical constants, such as double const PI = 3.14159 – realistically longer, or overall compile-time parameters. A const automatic variable (non-static local variable) means that single assignment is happening, though a different value may be used each time, such as int const x_squared = x * x. A const parameter in pass-by-reference means that the referenced value is not modified – it is part of the contract – while a const parameter in pass-by-value (or the pointer itself, in pass-by-reference) does not add anything to the interface (as the value has been copied), but indicates that internally, the function does not modify the local copy of the parameter (it is a single assignment). For this reason, some favor using const in parameters only for pass-by-reference, where it changes the contract, but not for pass-by-value, where it exposes the implementation.

C++

Methods

In order to take advantage of the design by contract approach for user-defined types (structs and classes), which can have methods as well as member data, the programmer may tag instance methods as const if they don't modify the object's data members. Applying the const qualifier to instance methods thus is an essential feature for const-correctness, and is not available in many other object-oriented languages such as Java and C# or in Microsoft's C++/CLI or Managed Extensions for C++. While const methods can be called by const and non-const objects alike, non-const methods can only be invoked by non-const objects. The const modifier on an instance method applies to the object pointed to by the " this " pointer, which is an implicit argument passed to all instance methods. Thus having const methods is a way to apply const-correctness to the implicit "this" pointer argument just like other arguments.

This example illustrates:

classC{inti;public:intGet()const// Note the "const" tag{returni;}voidSet(intj)// Note the lack of "const"{i=j;}};voidFoo(C&nonConstC,Cconst&constC){inty=nonConstC.Get();// Okintx=constC.Get();// Ok: Get() is constnonConstC.Set(10);// Ok: nonConstC is modifiableconstC.Set(10);// Error! Set() is a non-const method and constC is a const-qualified object}

In the above code, the implicit "this" pointer to Set() has the type "C *const"; whereas the "this" pointer to Get() has type "C const *const", indicating that the method cannot modify its object through the "this" pointer.

Often the programmer will supply both a const and a non-const method with the same name (but possibly quite different uses) in a class to accommodate both types of callers. Consider:

classMyArray{intdata[100];public:int&Get(inti){returndata[i];}intconst&Get(inti)const{returndata[i];}};voidFoo(MyArray&array,MyArrayconst&constArray){// Get a reference to an array element// and modify its referenced value.array.Get(5)=42;// OK! (Calls: int & MyArray::Get(int))constArray.Get(5)=42;// Error! (Calls: int const & MyArray::Get(int) const)}

The const-ness of the calling object determines which version of MyArray::Get() will be invoked and thus whether or not the caller is given a reference with which he can manipulate or only observe the private data in the object. The two methods technically have different signatures because their "this" pointers have different types, allowing the compiler to choose the right one. (Returning a const reference to an int, instead of merely returning the int by value, may be overkill in the second method, but the same technique can be used for arbitrary types, as in the Standard Template Library.)

Loopholes to const-correctness

There are several loopholes to pure const-correctness in C and C++. They exist primarily for compatibility with existing code.

The first, which applies only to C++, is the use of const_cast, which allows the programmer to strip the const qualifier, making any object modifiable. The necessity of stripping the qualifier arises when using existing code and libraries that cannot be modified but which are not const-correct. For instance, consider this code:

// Prototype for a function which we cannot change but which// we know does not modify the pointee passed in.voidLibraryFunc(int*ptr,intsize);voidCallLibraryFunc(intconst*ptr,intsize){LibraryFunc(ptr,size);// Error! Drops const qualifierint*nonConstPtr=const_cast<int*>(ptr);// Strip qualifierLibraryFunc(nonConstPtr,size);// OK}

However, any attempt to modify an object that is itself declared const by means of a const cast results in undefined behavior according to the ISO C++ Standard. In the example above, if ptr references a global, local, or member variable declared as const, or an object allocated on the heap via new int const, the code is only correct if LibraryFunc really does not modify the value pointed to by ptr.

The C language has a need of a loophole because a certain situation exists. Variables with static storage duration are allowed to be defined with an initial value. However, the initializer can use only constants like string constants and other literals, and is not allowed to use non-constant elements like variable names, whether the initializer elements are declared const or not, or whether the static duration variable is being declared const or not. There is a non-portable way to initialize a const variable that has static storage duration. By carefully constructing a typecast on the left hand side of a later assignment, a const variable can be written to, effectively stripping away the const attribute and 'initializing' it with non-constant elements like other const variables and such. Writing into a const variable this way may work as intended, but it causes undefined behavior and seriously contradicts const-correctness:

size_tconstbufferSize=8*1024;size_tconstuserTextBufferSize;//initial value depends on const bufferSize, can't be initialized here...intsetupUserTextBox(textBox_t*defaultTextBoxType,rect_t*defaultTextBoxLocation){*(size_t*)&userTextBufferSize=bufferSize-sizeof(structtextBoxControls);// warning: might work, but not guaranteed by C...}

Another loophole [11] applies both to C and C++. Specifically, the languages dictate that member pointers and references are "shallow" with respect to the const-ness of their owners – that is, a containing object that is const has all const members except that member pointees (and referees) are still mutable. To illustrate, consider this C++ code:

structS{intval;int*ptr;};voidFoo(Sconst&s){inti=42;s.val=i;// Error: s is const, so val is a const ints.ptr=&i;// Error: s is const, so ptr is a const pointer to int*s.ptr=i;// OK: the data pointed to by ptr is always mutable,//     even though this is sometimes not desirable}

Although the object s passed to Foo() is constant, which makes all of its members constant, the pointee accessible through s.ptr is still modifiable, though this may not be desirable from the standpoint of const-correctness because s might solely own the pointee. For this reason, Meyers argues that the default for member pointers and references should be "deep" const-ness, which could be overridden by a mutable qualifier when the pointee is not owned by the container, but this strategy would create compatibility issues with existing code. Thus, for historical reasons[ citation needed ], this loophole remains open in C and C++.

The latter loophole can be closed by using a class to hide the pointer behind a const-correct interface, but such classes either do not support the usual copy semantics from a const object (implying that the containing class cannot be copied by the usual semantics either) or allow other loopholes by permitting the stripping of const-ness through inadvertent or intentional copying.

Finally, several functions in the C standard library violate const-correctness before C23, as they accept a const pointer to a character string and return a non-const pointer to a part of the same string. strstr and strchr are among these functions. Some implementations of the C++ standard library, such as Microsoft's [12] try to close this loophole by providing two overloaded versions of some functions: a "const" version and a "non-const" version.

Problems

The use of the type system to express constancy leads to various complexities and problems, and has accordingly been criticized and not adopted outside the narrow C family of C, C++, and D. Java and C#, which are heavily influenced by C and C++, both explicitly rejected const-style type qualifiers, instead expressing constancy by keywords that apply to the identifier (final in Java, const and readonly in C#). Even within C and C++, the use of const varies significantly, with some projects and organizations using it consistently, and others avoiding it.

strchr problem

The const type qualifier causes difficulties when the logic of a function is agnostic to whether its input is constant or not, but returns a value which should be of the same qualified type as an input. In other words, for these functions, if the input is constant (const-qualified), the return value should be as well, but if the input is variable (not const-qualified), the return value should be as well. Because the type signature of these functions differs, it requires two functions (or potentially more, in case of multiple inputs) with the same logic – a form of generic programming.

This problem arises even for simple functions in the C standard library, notably strchr; this observation is credited by Ritchie to Tom Plum in the mid 1980s. [13] The strchr function locates a character in a string; formally, it returns a pointer to the first occurrence of the character c in the string s, and in classic C (K&R C) its prototype is:

char*strchr(char*s,intc);

The strchr function does not modify the input string, but the return value is often used by the caller to modify the string, such as:

if(p=strchr(q,'/'))*p=' ';

Thus on the one hand the input string can be const (since it is not modified by the function), and if the input string is const the return value should be as well – most simply because it might return exactly the input pointer, if the first character is a match – but on the other hand the return value should not be const if the original string was not const, since the caller may wish to use the pointer to modify the original string.

In C++ this is done via function overloading, typically implemented via a template, resulting in two functions, so that the return value has the same const-qualified type as the input: [lower-alpha 2]

char*strchr(char*s,intc);charconst*strchr(charconst*s,intc);

These can in turn be defined by a template:

template<T>T*strchr(T*s,intc){...}

In D this is handled via the inout keyword, which acts as a wildcard for const, immutable, or unqualified (variable), yielding: [14] [lower-alpha 3]

inout(char)*strchr(inout(char)*s,intc);

However, in C neither of these is possible since C does not have function overloading, and instead, this is handled by having a single function where the input is constant but the output is writable:

char*strchr(charconst*s,intc);

This allows idiomatic C code but does strip the const qualifier if the input actually was const-qualified, violating type safety. This solution was proposed by Ritchie and subsequently adopted. This difference is one of the failures of compatibility of C and C++.

Since C23, this problem is solved with the use of generic functions. strchr and the other functions affected by the issue will return a const pointer if one was passed to them and an unqualified pointer if an unqualified pointer was passed to them. [15]

D

In Version 2 of the D programming language, two keywords relating to const exist. [16] The immutable keyword denotes data that cannot be modified through any reference. The const keyword denotes a non-mutable view of mutable data. Unlike C++ const, D const and immutable are "deep" or transitive, and anything reachable through a const or immutable object is const or immutable respectively.

Example of const vs. immutable in D

int[]foo=newint[5];// foo is mutable.constint[]bar=foo;// bar is a const view of mutable data.immutableint[]baz=foo;// Error:  all views of immutable data must be immutable.immutableint[]nums=newimmutable(int)[5];// No mutable reference to nums may be created.constint[]constNums=nums;// Works.  immutable is implicitly convertible to const.int[]mutableNums=nums;// Error:  Cannot create a mutable view of immutable data.

Example of transitive or deep const in D

classFoo{Foonext;intnum;}immutableFoofoo=newimmutable(Foo);foo.next.num=5;// Won't compile.  foo.next is of type immutable(Foo).// foo.next.num is of type immutable(int).

History

const was introduced by Bjarne Stroustrup in C with Classes, the predecessor to C++, in 1981, and was originally called readonly. [17] [18] As to motivation, Stroustrup writes: [18]

"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."

The first use, as a scoped and typed alternative to macros, was analogously fulfilled for function-like macros via the inline keyword. Constant pointers, and the * const notation, were suggested by Dennis Ritchie and so adopted. [18]

const was then adopted in C as part of standardization, and appears in C89 (and subsequent versions) along with the other type qualifier, volatile. [19] 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 keyword in C99. Ritchie was not very supportive of these additions, arguing that they did not "carry their weight", but ultimately did not argue for their removal from the standard. [20]

D subsequently inherited const from C++, where it is known as a type constructor (not type qualifier) and added two further type constructors, immutable and inout, to handle related use cases. [lower-alpha 4]

Other languages

Other languages do not follow C/C++ in having constancy part of the type, though they often have superficially similar constructs and may use the const keyword. Typically this is only used for constants (constant objects).

C# has a const keyword, but with radically different and simpler semantics: it means a compile-time constant, and is not part of the type.

Nim has a const keyword similar to that of C#: it also declares a compile-time constant rather than forming part of the type. However, in Nim, a constant can be declared from any expression that can be evaluated at compile time. [21] In C#, only C# built-in types can be declared as const; user-defined types, including classes, structs, and arrays, cannot be const. [22]

Java does not have const – it instead has final, which can be applied to local "variable" declarations and applies to the identifier, not the type. It has a different object-oriented use for object members, which is the origin of the name.

The Java language specification regards const as a reserved keyword – i.e., one that cannot be used as variable identifier – but assigns no semantics to it: it is a reserved word (it cannot be used in identifiers) but not a keyword (it has no special meaning). The keyword was included as a means for Java compilers to detect and warn about the incorrect usage of C++ keywords. [23] An enhancement request ticket for implementing const correctness exists in the Java Community Process, but was closed in 2005 on the basis that it was impossible to implement in a backwards-compatible fashion. [24]

The contemporary Ada 83 independently had the notion of a constant object and a constant keyword, [25] [lower-alpha 5] with input parameters and loop parameters being implicitly constant. Here the constant is a property of the object, not of the type.

JavaScript has a const declaration that defines a block-scoped variable that cannot be reassigned nor redeclared. It defines a read-only reference to a variable that cannot be redefined, but in some situations the value of the variable itself may potentially change, such as if the variable refers to an object and a property of it is altered. [26]

See also

Notes

  1. Formally when the const is part of the outermost derived type in a declaration; pointers complicate discussion.
  2. Note that pointer declaration syntax conventions differ between C and C++: in C char *s is standard, while in C++ char* s is standard.
  3. Idiomatic D code would use an array here instead of a pointer. [14]
  4. D also introduced the shared type constructor, but this is related to use cases of volatile, not const.
  5. The Ada standard calls this a "reserved word"; see that article for usage.

Related Research Articles

In object-oriented (OO) and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created. In some cases, an object is considered immutable even if some internally used attributes change, but the object's state appears unchanging from an external point of view. For example, an object that uses memoization to cache the results of expensive computations could still be considered an immutable object.

<span class="mw-page-title-main">C syntax</span> Set of rules defining correctly structured programs

The syntax of the C programming language is the set of rules governing writing of software in C. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.

<span class="mw-page-title-main">Pointer (computer programming)</span> Object which stores memory addresses in a computer program

In computer science, a pointer is an object in many programming languages that stores a memory address. This can be that of another value located in computer memory, or in some cases, that of memory-mapped computer hardware. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer. As an analogy, a page number in a book's index could be considered a pointer to the corresponding page; dereferencing such a pointer would be done by flipping to the page with the given page number and reading the text found on that page. The actual format and content of a pointer variable is dependent on the underlying computer architecture.

A function pointer, also called a subroutine pointer or procedure pointer, is a pointer referencing executable code, rather than data. Dereferencing the function pointer yields the referenced function, which can be invoked and passed arguments just as in a normal function call. Such an invocation is also known as an "indirect" call, because the function is being invoked indirectly through a variable instead of directly through a fixed identifier or address.

In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C. The name C++ reference may cause confusion, as in computer science a reference is a general concept datatype, with pointers and C++ references being specific reference datatype implementations. The definition of a reference in C++ is such that it does not need to exist. It can be implemented as a new name for an existing object.

<span class="mw-page-title-main">Java syntax</span> Set of rules defining correctly structured program

The syntax of Java is the set of rules defining how a Java program is written and interpreted.

The computer programming languages C and Pascal have similar times of origin, influences, and purposes. Both were used to design their own compilers early in their lifetimes. The original Pascal definition appeared in 1969 and a first compiler in 1970. The first version of C appeared in 1972.

In the C programming language, data types constitute the semantics and characteristics of storage of data elements. They are expressed in the language syntax in form of declarations for memory locations or variables. Data types also determine the types of operations or methods of processing of data elements.

In the Java programming language, the final keyword is used in several contexts to define an entity that can only be assigned once.

typedef is a reserved keyword in the programming languages C, C++, and Objective-C. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type. As such, it is often used to simplify the syntax of declaring complex data structures consisting of struct and union types, although it is also commonly used to provide specific descriptive type names for integer data types of varying sizes.

In computer programming, a forward declaration is a declaration of an identifier for which the programmer has not yet given a complete definition.


A class in C++ is a user-defined type or data structure declared with any of the keywords class, struct or union that has data and functions as its members whose access is governed by the three access specifiers private, protected or public. By default access to members of a C++ class declared with the keyword class is private. The private members are not accessible outside the class; they can be accessed only through member functions of the class. The public members form an interface to the class and are accessible outside the class.

The C and C++ programming languages are closely related but have many significant differences. C++ began as a fork of an early, pre-standardized C, and was designed to be mostly source-and-link compatible with C compilers of the time. Due to this, development tools for the two languages are often integrated into a single product, with the programmer able to specify C or C++ as their source language.

C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.

This is an overview of Fortran 95 language features. Included are the additional features of TR-15581:Enhanced Data Type Facilities, which have been universally implemented. Old features that have been superseded by new ones are not described – few of those historic features are used in modern programs although most have been retained in the language to maintain backward compatibility. The current standard is Fortran 2023; many of its new features are still being implemented in compilers.

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

This article compares a large number of programming languages by tabulating their data types, their expression, statement, and declaration syntax, and some common operating-system interfaces.

In computer programming, a constant is a value that is not altered by the program during normal execution. When associated with an identifier, a constant is said to be "named," although the terms "constant" and "named constant" are often used interchangeably. This is contrasted with a variable, which is an identifier with a value that can be changed during normal execution. To simplify, constants' values remains, while the values of variables varies, hence both their names.

C++14 is a version of the ISO/IEC 14882 standard for the C++ programming language. It is intended to be a small extension over C++11, featuring mainly bug fixes and small improvements, and was replaced by C++17. Its approval was announced on August 18, 2014. C++14 was published as ISO/IEC 14882:2014 in December 2014.

In the C, C++, and D programming languages, 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.

References

  1. "Constant items – The Rust Reference". doc.rust-lang.org. Retrieved 2022-06-22.
  2. "The this pointer". Draft C++ Standard. Retrieved 2020-03-30. The type of this in a member function whose type has a cv-qualifier-seq cv and whose class is X is "pointer to cvX".
  3. Herb Sutter and Andrei Alexandrescu (2005). C++ Coding Standards. p. 30. Boston: Addison Wesley. ISBN   0-321-11358-6
  4. "Why is the kfree() argument const?". lkml.org. 2013-01-12.
  5. "5.1. Extensions implemented in GNU Fortran: 5.1.16 Cray pointers". The GNU Fortran Compiler. 2006. Archived from the original on 2022-12-21. Retrieved 2022-12-21.
  6. Fahey, Mark R.; Nagle, Dan (1999-04-19). "Cray Fortran Pointers vs. Fortran 90 Pointers and Porting from the Cray C90 to the SGI Origin2000" (PDF). Vicksburg, Massachusetts, USA: US Army Corps of Engineers Waterways Experiment Station, Major Shared Resource Center. Archived (PDF) from the original on 2022-12-23. Retrieved 2022-12-23. (8 pages)
  7. "Appendix C: Fortran 90 Features and Differences > Features > Cray Pointers". Fortran User's Guide. Oracle Corporation. 2010. Archived from the original on 2021-09-21. Retrieved 2022-12-23.
  8. "Appendix C: Fortran 90 Features and Differences > Features > Cray Character Pointers". Fortran User's Guide. Oracle Corporation. 2010. Archived from the original on 2022-12-23. Retrieved 2022-12-23.
  9. "Chapter 4. Data Types". Fortran Language Reference Manual, Volume 1. Vol. 1. Silicon Graphics, Inc. 1999 [1993]. Document Number: 007-3692-004. Archived from the original on 2022-12-23. Retrieved 2022-12-23. (NB. Derived from "FORTRAN 90 HANDBOOK" (1992, McGraw-Hill, Inc.) by Walter S. Brainerd, Jeanne C. Adams, Jeanne T. Martin, Brian T. Smith, and Jerrold L. Wagener.)
  10. "Stroustrup: C++ Style and Technique FAQ".
  11. Scott Meyers (2005). Effective C++, Third Edition. pp. 21–23. Boston: Addison Wesley. ISBN   978-0-321-33487-9
  12. "strchr, wcschr, _mbschr (CRT)". Msdn.microsoft.com. Retrieved 2017-11-23.
  13. "Dennis Ritchie: Why I do not like X3J11 type qualifiers".
  14. 1 2 The D Programming Language, Andrei Alexandrescu, 8.8: Propagating a Qualifier from Parameter to Result
  15. "WG14-N3020 : Qualifier-preserving standard library functions" (PDF). open-std.org. 2022-06-13. Archived (PDF) from the original on 2022-10-13.
  16. "const(FAQ) – D Programming Language". Digitalmars.com. Retrieved 2013-08-18.
  17. Bjarne Stroustrup, "Extensions of the C Language Type Concept", Bell Labs internal Technical Memorandum, January 5, 1981.
  18. 1 2 3 Sibling Rivalry: C and C++, Bjarne Stroustrup, 2002, p. 5
  19. Dennis M. Ritchie, "The Development of the C Language Archived July 15, 2012, at archive.today ", 2003: "X3J11 also introduced a host of smaller additions and adjustments, for example, the type qualifiers const and volatile, and slightly different type promotion rules."
  20. "Let me begin by saying that I'm not convinced that even the pre-December qualifiers ('const' and 'volatile') carry their weight; I suspect that what they add to the cost of learning and using the language is not repaid in greater expressiveness. 'Volatile', in particular, is a frill for esoteric applications, and much better expressed by other means. Its chief virtue is that nearly everyone can forget about it. 'Const' is simultaneously more useful and more obtrusive; you can't avoid learning about it, because of its presence in the library interface. Nevertheless, I don't argue for the extirpation of qualifiers, if only because it is too late."
  21. Nim Manual: Const section
  22. const (C# Reference)
  23. Gosling, James; Joy, Bill; Steele, Guy. "Java Language Specification Third Edition".
  24. "Bug ID: JDK-4211070 Java should support const parameters (like C++) for code maintainence[sic]". Bugs.sun.com. Retrieved 2014-11-04.
  25. 1815A [ dead link ], 3.2.1. Object Declarations Archived October 20, 2014, at the Wayback Machine :
    "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."
  26. "const". MDN. Retrieved 2017-10-31.