Section 1.4 Symbolic Constants

Program

#include <stdio.h>
#define LOWER 0 /* lower limit of table */
#define UPPER 300 /* upper limit */
#define STEP 20 /* step size */
/* print Fahrenheit-Celsius table */
int main()
{
    int fahr;
    for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
        printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}
Run this

Explanation

In this program we are going to convert a given Fahrenheit temperature to Celsius temperature using the formula C=(5/9)(F-32). We define some some symbolic constants in the beginning of the program so that they can be used in the later stages of the program. The constants that are defined in the program are: LOWER,UPPER,STEP, . The label LOWER is assigned the value 0 similarly UPPER to 300, STEP to 20. So when the program enters the for loop it checks whether fahr <= UPPER, and the increments fahr using STEP in each iteration.

symbolic constants are substituted inline in the program during pre-processing phase of compilation.


This document was updated on 16 Nov of 17

Comments by Disqus