Course Content
Programming in C
About Lesson

You may have noticed that we use the number “10” twice in our multiplication table program (see the previous two sections) in the conditional checks. It’s a bad practice to bury “magic numbers” like 10 in a program; they convey little information to someone who might have to read the program later, and they are hard to change in a systematic way. A #define line defines a symbolic name or symbolic constant to be a particular string of characters:

#define name replacement_text

Thereafter, any occurrence of name (not in quotes and not part of another name) will be replaced by the corresponding replacement_text. The name has the same form as a variable name: a sequence of letters and digits that begin with a letter. The replacement_text can be any sequence of characters, it is not limited to numbers.

#include <stdio.h>

#define LAST 10
main()
{
    int num, times, prod;
    for (num = 1; num <= LAST; num++) {
        printf("Table of %d\n", num);
        for (times = 1; times <= LAST; times++) {
            prod = num * times;
            printf("%2d x %2d = %3d\n", num, times, prod);
        }
    }
}

The quantity LAST is a symbolic constant, not a variable, so it does not appear in declarations. Symbolic constant names are conventionally written in upper case so they can be readily distinguished from lower case variable names. Notice that there is no semicolon at the end of a #define line.

Scroll to Top