A constant, in programming, it is defined as a fixed value that a program cannot modify while it is running. These fixed values are also known as literals. It is treated as a common variable, except that its value cannot be altered after it has been defined.
Constants can have any of the basic data types, such as an integer, float, character, or string constant. It is equal to a variable in its declaration and in the ability to look at the value it has stored within it. However, its value cannot be changed while the program is running.
If you try to change the value of a constant, an error will appear. Therefore, it is very useful for values that rarely or do not change, such as VAT, Pi, etc..
By using a constant, there is no risk that its value could be accidentally changed. For example, you would not want the value of Pi to be accidentally changed to 4, since all calculations involving Pi would go wrong.
Article index
In mathematics, a constant is a specific number or symbol that is assigned a fixed value. For example, in the equation: y = 3z-2, “y” and “z” are variables, while the numbers 3 and 2 are constants.
Constants are used in programming to store fixed values. They are of interest to define values that will be used several times within a program. For example, below, "mini" and "maxi" are declared as constants.
- const int mini = 20.
- const int maxi = 200.
Thanks to the use of constants, several instances of a value can be changed simultaneously. For example, when modifying the value assigned to maxi in the example, that value will be changed whenever maxi is referenced.
If the number 200 were used instead of maxi, the programmer would have to modify each individual instance of "200". Therefore, it is considered good programming practice to use constants every time a fixed value is used multiple times..
The constants provide the guarantee that the code will not be able to change its value. This is not very important for a small project, but it does matter a lot in a large project with multiple components written by multiple programmers..
The constants also provide a hint to the compiler for optimization. Since the compiler knows that that value cannot change, it does not need to load the value into memory, optimizing the code to work only for the value of the constant.
A variable, as its name implies, varies eventually. However, if it does not change, nothing happens. As readability is also important, whenever possible you should explicitly use a constant and leave the variables for the values that can actually change.
You will surely know what the value 3.14 means. However, not everyone will know that 3.05 is a tax rate in a state. Therefore, not everyone who does future code maintenance will know..
If the tax rate changes in the future, it will be annoying to have to change each value from 3.05 to 3.16. Therefore, changes are minimized by making only one change in the constant.
The Boolean data type can only have one of the following two values: 0 (False) and 1 (True).
An integer literal is a sequence of digits. It can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or root: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
An integer literal can also have a suffix that is a combination of U and L, for "unsigned" and "long", respectively. The suffix can be uppercase or lowercase, and can be in any order.
A floating point constant is a number that is made up of an integer part, a decimal point, and a fractional part. These constants can be displayed in both decimal and exponential form..
When representing decimal form, you must include the decimal point, the exponent, or both. When the exponential form is represented, the integer part, the fractional part, or both must be included. The exponent with its sign is introduced by an e or E.
A character literal can be a character (for example, 'k'), an escape sequence (for example, '\ t'), or a universal character (for example, '\ u02C1').
This type of constant is enclosed in single quotes. For example, 'h' or '3' can be stored in a constant of type char. The maximum size of this type of constant is one byte.
Some characters in C have a special meaning when they are preceded by a backslash. For example, newline (\ n) or tab (\ t).
A character string contains a set of characters with characteristics of type character. String constants are enclosed in double quotes "".
A long line can be broken into multiple lines by using string constants and separating them with blank spaces. For example, the following two forms are the same character strings:
- "Hello dear".
- "Hello dear".
It is an unnamed constant that is used to specify data. Literal constants are encoded as part of a statement. For example, in the sentence a = b + 6 the literal constant '6' was used.
They are usually placed at the beginning of the program, although they could be placed anywhere. They are defined as follows, for example: #define Year 2019.
The Year value can be changed quickly by being placed at the beginning of the program, as it can be easily found.
They use a constant type qualifier to indicate that the data cannot be changed. The syntax is: const type identifier = value. An example would be: const float PI = 3.1415.
The following program shows the three different ways to encode PI as a constant:
You can use the prefix "const" to declare constants with a specific data type, as follows: const variable type = value. With the following example you can see in more detail:
When the above code is compiled and executed, the following result occurs: Area value: 50.
It should be noted that it is good programming practice to always define constants in Uppercase.
Yet No Comments