Symbolic Constants

  • The numbers 0, 20, and 300 in the program mean very little to readers of the program unless they are very familiar with what the program is doing
  • for (fahr=0; fahr <= 300; fahr = fahr+20)
  • C allows the definition of symbolic constants - names that will be replaced with their values when the program is compiled
  • Symbolic constants are defined before main(), and the syntax is
  • Example:

            // program name: temp_conv2.c

            #include <stdio.h>
            #define LOWER   0   /* lower limit of table */
            #define UPPER   300 /* upper limit */
            #define STEP    20  /* step size */
                   int fahr;
                   for (fahr=LOWER; fahr<=UPPER; fahr=fahr+STEP)
                           printf("%3d %6.1f\n",fahr,(5.0/9.0)*(fahr-32));
  • There is no semi-colon after the definition of a symbolic constant
  • You cannot change the value of a symbolic constant at run-time