C++ has two kinds of constants: literal, and symbolic.
Literal constants
Literal constants are literal numbers inserted into the code. They are constants because you can’t change their values.
int x = 5; // 5 is a literal constant
Literal constants can have suffixes that determine their types. Integer constants can have a u or U suffix that means they are unsigned. Integer constants can also have a l or L suffix, which means they are long integers. However, these suffixes are typically optional, as the compiler can usually tell from context what kind of constant you need.
int nValue = 5u; // unsigned constant long nValue2 = 5L; // long constant
By default, floating point literal constant have a type of double. To convert them into a float value, the f or F suffix can be used:
float fValue = 5.0f; // float constant
Floating point literal constants can also use an l or L suffix to make them long doubles.
Generally, it is a good idea to try to avoid using literal constants that aren’t 0 or 1. For more detail, you can review the section on magic numbers, and why they are a bad idea.
Symbolic constants
As you learned in a previous lesson, you can use the #define preprocessor directive in order to declare a symbolic constant:
#define YEN_PER_DOLLAR 122 int nYen = nDollars * YEN_PER_DOLLAR;
There are two major problems with symbolic constants declared using #define. First, because they are resolved by the preprocessor, which replaces the symbolic name with the defined value, #defined symbolic constants do not show up in the debugger. Thus, if you only saw the statement int nYen = nDollars * YEN_PER_DOLLAR;, you would have to go looking for the #define declaration in order to find out what value of YEN_PER_DOLLAR was used.
Second, #defined values always have global scope (which we’ll talk about in the section on local and global variables). This means a value #defined in one piece of code may have a naming conflict with a value #defined with the same name in another piece of code.
A better way to do symbolic constants is through use of the const keyword. Const variables must be assigned a value when declared, and then that value can not be changed. Here is the way the above snippet of code should be written:
const int nYenPerDollar = 122; int nYen = nDollars * nYenPerDollar;
Declaring a variable as const prevents us from inadvertently changing it’s value:
const int nYenPerDollar = 122; nYenPerDollar = 123; // compiler error!
Although a constant variable might seem like an oxymoron, they can be very useful in helping to document your code and avoid magic numbers. Some programmers prefer to use all upper-case names for const variables (to match the style of #defined values). However, we will use normal variable naming conventions, which is more common. Const variables act exactly like normal variables in every case except that they can not be assigned to.
2.9 — Hungarian Notation
|
Index
|
2.7 — Chars
|
2.9 — Hungarian Notation
Index
2.7 — Chars
Could you not #define nYenPerDollar in a header file which is included in the main source code?
This would eliminate the inconvenience of having to search for its place of definition and would also prevent the confict you mentioned.
– Just a thought. I’m completely new to C++ and object-oriented programming all together, just trying to get my head around how many different ways the same task can be completed in C++.
PS: I am loving this tutorial. I’m using it for m pre-University study. =D
You could #define nYenPerDollar in a header file, but as I mentioned in the tutorial, #define values don’t show up in the debugger, which can make them hard to debug, especially if you code uses them heavily. Since the concept of yen per dollar is a global concept (it’s going to remain constant throughout the program) the fact that all #define values are global isn’t much of a problem in this case.
Literal constants are literal numbers inserted into the code. They are constants because you can’t change their values.
int x = 5; // 5 is a literal constant
I don’t understand this, you can change 5 to 6, how is it unchangable?
If you use the number 6 instead of 5, you are using a different literal, not changing the value of a literal. In other words, literals are constants because the symbol 5 always has the value 5. You can’t change the symbol 5 to the value 6, or any other number.
Alex:
Ref.: it is a good idea to try to avoid using literal constants that aren’t 0 or 1.
So I should use only literal constants that are 0 or 1?¿? As for above.
Generally I only use the literal constant 0 — anything else is either defined as a #define value, a const int, or an enum.
[...] 2007 Prev/Next Posts « 2.8 — Constants | Home | 2.10 — Comprehensive quiz » Monday, June 11th, 2007 at 4:04 [...]
[...] 2007 Prev/Next Posts « 2.6 — Boolean Values | Home | 2.8 — Constants » Saturday, June 9th, 2007 at 7:07 [...]
Did constants apply through the whole file? Or what? :S
^ Ups I forgot to specify, I mean const values using the
keyword.