In mathematics, a constant is a specific number or a symbol that is assigned a fixed value. For example, in the equation below, "y" and "x" are variables, while the numbers 2 and 3 are constants.
y = 2x - 3
Constants are also used in computer programming to store fixed values. They are typically declared at the top of a source code file or at the beginning of a function. Constants are useful for defining values that are used many times within a function or program. For example, in the C++ code below, min and max are declared as constants.
const int min = 10;
const int max = 100;
By using constants, programmers can modify multiple instances of a value at one time. For example, changing the value assigned to max in the example above will modify the value wherever max is referenced. If the number 100 was used instead of max, the programmer would have to modify each instance of "100" individually. Therefore, it is considered good programming practice to use constants whenever a fixed value is used multiple times.
Updated: May 3, 2012