Conditional Compilation

Chapter 6 - Working with Data

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Access Modifiers
The const and volatile modifiers are new to C and C++. They were added by the ANSI C standard to help identify which variables will never change (const) and which can change unexpectedly (volatile).
const Modifier
At certain times you will need to use a value that does not change throughout the program. Such a quantity is called a constant. For example, if a program deals with the area and circumference of a circle, the constant value pi=3.14159 would be used frequently. In a financial program, an interest rate might be a constant. In such cases, you can improve the readability of the program by giving the constant a descriptive name.
Using descriptive names can also help prevent errors. Suppose that a constant value (not a constant variable) is used at many points throughout the program. A typographical error might result in the wrong value being typed at one or more of these points. However, if the constant is given a name, a typographical error would then be detected by the compiler because the incorrectly spelled identifier would probably not have been declared.
Suppose you are writing a program that repeatedly uses the value pi. It might seem as though a variable called pi should be declared with an initial value of 3.14159. However, the program should not be able to change the value of a constant. For instance, if you inadvertently wrote “pi” to the left of an equal sign, the value of pi would be changed, causing all subsequent calculations to be in error. C and C++ provide mechanisms that prevent such an error from occurring: you can establish constants, the values of which cannot be changed.
In C and C++, you declare a constant by writing “const” before the keyword (such as int, float, or double) in the declaration. For example:
const int iMIN=1,iSALE_PERCENTAGE=25;
const float fbase_change=32.157;
int irow_index=1,itotal=100,iobject;
double ddistance=0,dvelocity;
Because a constant cannot be changed, it must be initialized in its declaration. The integer constants iMIN and iSALE_PERCENTAGE are declared with values 1 and 25, respectively; the constant fbase_change is of type float and has been initialized to 32.157. In addition, the integer (nonconstant) variables irow_index, itotal, and iobject have been declared. Initial values of 1 and 100 have been established for irow_index and itotal, respectively. Finally, ddistance and dvelocity have been declared to be (nonconstant) variables of type double. An initial value of zero has been set up for ddistance.
Constants and variables are used in the same way in a program. The only difference is that the initial values assigned to the constants cannot be changed. That is, the constants are not lvalues; they cannot appear to the left of an equal sign. (Expressions that refer to memory locations are called lvalue expressions. Expressions referring to modifiable locations are modifiable lvalues. One example of a modifiable lvalue expression is a variable name declared without the const specifier.)
Normally, the assignment operation assigns the value of the right-hand operand to the storage location named by the left-hand operand. Therefore, the left-hand operand of an assignment operation (or the single operand of a unary assignment expression) must be an expression that refers to a modifiable memory location.
#define Constants
C and C++ provide another method for establishing constants: the #define compiler directive. Let’s look at an example. Suppose that at the beginning of a program you have the statement:
#define SALES_TEAM 10
The form of this statement is #define followed by two strings of characters separated by blanks. When the program is compiled, there are several passes made through it. The first step is accomplished by the compiler preprocessor, which does such things as carry out the #include and #define directives. When the preprocessor encounters the #define directive, it replaces every occurrence of SALES_TEAM in the source file(s) with the number 10.
In general, when the preprocessor encounters a #define directive, it replaces every occurrence of the first string of characters, “SALES_TEAM,” in the program with the second string of characters, “10.” Additionally, no value can be assigned to SALES_TEAM because it has never been declared to be a variable. As a result of the syntax, SALES_TEAM has all the attributes of a constant. Note that the #define statement is not terminated by a semicolon. If a semicolon followed the value 10, then every occurrence of SALES_TEAM would be replaced with “10";. The directive’s action is to replace the first string with everything in the second string.
All of the programs that have been discussed so far are short, and would usually be stored in a single file. If a statement such as the #define for SALES_TEAM appeared at the beginning of the file, the substitution of “10" for ”SALES_TEAM" would take place throughout the program. (A later chapter of this book discusses breaking a program down into many subprograms, with each subprogram being broken down into separate files.) Under these circumstances, the compiler directive would be effective only for the single file in which it is written.
The preceding discussion explored two methods for defining constants—the keyword const and the #define compiler directive. In many programs, the action of each of these two methods is essentially the same. On the other hand, the use of the modifier keyword const results in a “variable” whose value cannot be changed. Later in this chapter, in “Storage Classes,” you will see how variables can be declared in such a way that they exist only over certain regions of a program. The same can be said for constants declared with the keyword const. Thus, the const declaration is somewhat more versatile than the #define directive. Also, the #define directive is found in standard C and is therefore already familiar to C programmers.
volatile Modifier
The volatile keyword signifies that a variable can unexpectedly change because of events outside the control of the program. For example, the following definition indicates that the variable event_time can have its value changed without the knowledge of the program:
volatile int event_time;
A definition like this is needed, for example, if event_time is updated by hardware that maintains the current clock time. The program that contains the variable event_time could be interrupted by the timekeeping hardware and the variable event_time changed.
A data object should be declared volatile if it is a memory-mapped device register or a data object shared by separate processes, as would be the case in a multitasking operating environment.
const and volatile Used Together
You can use the const and volatile modifiers with any other data types (for example, char and float) and also with each other. The following definition specifies that the program does not intend to change the value in the variable constant_event_time:
const volatile constant_event_time;
However, the compiler is also instructed, because of the volatile modifier, to make no assumptions about the variable’s value from one moment to the next. Therefore, two things happen. First, an error message will be issued by the compiler for any line of source code that attempts to change the value of the variable constant_event_time. Second, the compiler will not remove the variable constant_event_time from inside loops since an external process can also be updating the variable while the program is executing.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net