Home Segments Index Top Previous Next

137: Mainline

Now suppose that you want to write many functions that involve, say, the minimum return on investment you are willing to accept. You decide that you can fix the minimum return before you compile your program, and not only do you never expect to change that minimum return rate, but also you want the C compiler to stop you if you try to change it.

You could, of course, decide that the minimum return rate is a rate such as 2.5, and use 2.5 explicitly wherever you need the minimum return rate. That would make your program hard to modify, however, should your expectations ever change.

Plainly, you need a way to express constants symbolically such that you can change their values but your program cannot. In C, the approved approach is to define what is called a macro symbol. Whenever you define a macro symbol, you tell C that you want a symbol, such as MINIMUM_RETURN_RATE, to be replaced by a character string, such as 2.5, wherever the symbol appears in your program. This substitution takes place before the main work of compilation begins.

To declare a macro symbol, you include a line such as the following in your program:

                  *-- Symbol to be substituted for 
                  |          
                  |          *-- Text to be substituted 
                  |          |  
                  v          v  
        ------------------- --- 
#define MINIMUM_RETURN_RATE 2.5 

Note that, by convention, macro symbols contain only upper-case characters. Note also that macro-symbol declarations exhibit no equal sign and no semicolon.