In computer programming, a constant is a special kind of variable whose value cannot normally be altered during program execution. Many programming languages make an explicit syntactic distinction between constant and variable symbols.
Although a constant's value is specified only once, a constant may be referenced many times in a program. Using a constant instead of specifying a value multiple times in the program can not only simplify code maintenance, but it can also supply a meaningful name for it and consolidate such constant bindings to a standard code location (for example, at the beginning).
Contents |
Comparison with literals and macros
There are several main ways to express a data value that doesn't change during program execution which are consistent across a wide variety of programming languages. One very basic way is by simply writing a literal number, character, or string into the program code, which is straightforward in C, C++, and similar languages.
In assembly language, literal numbers and characters are done using the "immediate mode" instructions available on most microprocessors. The name "immediate" comes from the values being available immediately from the instruction stream, as opposed to loading them indirectly by looking up a memory address.[1] On the other hand, values longer than the microprocessor's word length, such as strings and arrays, are handled indirectly and assemblers generally provide a "data" pseudo-op to embed such data tables in a program.
Another way is by defining a symbolic macro. Most high-level programming languages, and many assemblers, offer a macro facility where the programmer can define, generally at the beginning of a source file or in a separate header file, names for different values. A preprocessor then replaces these names with the appropriate values before compiling, resulting in something functionally identical to using literals, with the speed advantages of immediate mode. Because it can be difficult to maintain code where all values are written literally, if a value is used in any repetitive or non-obvious way, it is often done as a macro.
A third way is by declaring and defining a constant variable. A global or static variable can be declared ( or a symbol defined in assembly ) with the qualifier "const", meaning that its value will be set at compile time and should not be changeable at runtime. Compilers generally put static constants in the .text section of the object file along with the code itself, as opposed to the .data section where non-const initialized data is kept, though some have an option to produce a .const section if so desired. Memory protection can be applied to this area to prevent overwriting of const variables by errant pointers.
These "constant variables" differ from literals in a number of ways. Compilers generally place a const variable in a single memory location identified by symbol, rather than spread throughout the executable as with a macro. While this precludes the speed advantages of immediate mode, there are advantages in memory efficiency, and debuggers can work with these constants at runtime. Also while macros may be redefined accidentally by conflicting header files, conflicting constants are detected at compile time.
Dynamic constants
Besides the static constants described above, many procedural languages such as C extend the concept of const-ness toward local variables that are automatically created at runtime on the stack or in registers, to dynamically allocated memory that is accessed by pointer, and to parameter lists in function headers.
These dynamic constants don't designate a variable as residing in a specific region of memory, nor are the values set at compile time. Expressions such as "const a = b + 20" are valid within a function when 'a' is a local variable, even if b is not const. Here, the const is used as a signal to the compiler that this variable is meant to be written only once, at creation.
Const is often used in function headers, as a promise that when an object is passed by reference, the called function will not change it. Depending on the syntax, either a pointer or the object being pointed to may be const, however normally the latter is desired. The discipline of ensuring that the proper data structures are const throughout the program is called const-correctness.
Object-oriented constants
A const data structure or object is referred to as "immutable" in object-oriented parlance. An object being immutable confers some advantages in program design. For instance, it may be "copied" simply by copying its pointer or reference, avoiding a time-consuming copy operation and conserving memory.
Object-oriented languages such as C++ extend const even further. Individual members of a struct or class may be made const even if the class isn't. Conversely, the "mutable" keyword allows a class member to be changed even if an object was instantiated as const.
Even functions can be const in C++. The meaning here is that only a const function may be called for an object instantiated as const; a const function doesn't change any non-mutable data.
Java has a qualifier called "final" which means that a variable can be written to once, though it doesn't provide all the functionality of C++'s const. C# has both a "const" and a "readonly" qualifier; their const is only for compile-time constants, while readonly can be used in constructors and other runtime applications.
Naming conventions
Naming conventions for constant variables vary. Some simply name them as they would any other variable. Others use capitals and underscores for constants in a way similar to their traditional use for symbolic macros (e.g SOME_CONSTANT).[2] In Hungarian notation, a "k" prefix signifies constants as well as macros and enumerated types.
References
- ^ Ex. IBM Systems Information. Instruction Set - Assembler Language Reference for PowerPC.
- ^ Microsoft Office XP Developer: Constant Names
See also
This entry is from Wikipedia, the leading user-contributed encyclopedia. It may not have been reviewed by professional editors (see full disclaimer)




