How to Declare and Use Constants in the C Language

Constants can be very useful in C programming whenever you have any value that is repeated in your program. Declaring a constant allows you to quickly and easily change a value that is used throughout your code simply by changing the declaration.

Declaring constants

Unlike variables, constants are declared using the C language preprocessor and not the compiler. Here's the format:

#define VOTING_AGE 18

The #define directive is a preprocessor command. It's followed by the name of the symbol being defined, VOTING_AGE. These symbols are named like variables, though using ALL CAPS for constants lets you easily identify constants versus variables in your source code. The symbol must be all one word. Following the symbol is a space and then the value that the symbol represents. There is no equal sign!

Don't include a semicolon unless you want the semicolon to be a part of the constant (and you probably don't).

The preprocessor takes the symbol and does a search-and-replace operation throughout your code, substituting the symbol with whatever it represents. In the end, the "constant" becomes an immediate value. And, by using #define to create the constant, you give yourself the flexibility to easily change it later.

Using constants in your code

Constant definitions typically follow the #include directives at the top of C source code:

#include <stdio.h>
#define SPEEDLIMIT 55
#define RATE 15
#define FIRST_TICKET 85
#define SECOND_TICKET 95
#define THIRD_TICKET 100
int main()
{
    int total,fine,speeding;
    puts("Speeding Tickets\n");
/* first ticket */
    speeding = FIRST_TICKET - SPEEDLIMIT;
    fine = speeding * RATE;
    total = total + fine;
    printf("For going %d in a %d zone: $%d\n", FIRST_TICKET,SPEEDLIMIT,fine);
/* second ticket */
    speeding = SECOND_TICKET - SPEEDLIMIT;
    fine = speeding * RATE;
    total = total + fine;
    printf("For going %d in a %d zone: $%d\n", SECOND_TICKET,SPEEDLIMIT,fine);
/* third ticket */
    speeding = THIRD_TICKET - SPEEDLIMIT;
    fine = speeding * RATE;
    total = total + fine;
    printf("For going %d in a %d zone: $%d\n", THIRD_TICKET,SPEEDLIMIT,fine);
/* Display total */
   printf("\nTotal in fines: $%d\n",total);
   return(0);
}

Type this into your editor, save it, compile it, and run it.

Suppose that you have completed the program, but then the local council changes the speed limit from 55 mph to 60 mph. Also, the fine has jumped from $15 to $26 for every mile per hour you speed over the limit. Because you used constants, you can make those changes in one place and have them trickle down to the entire code.

In this case, you would change your constant declarations to this:

#define SPEEDLIMIT 60
#define RATE 26

Other things you can #define

The #define directive isn't limited to merely creating constants for you. You can define anything with the #define directive, essentially replacing major pieces of your program with whatever you want. A good example of when you might want to do this may be something like this:

#define ASK_PROMPT printf("Do You Want to Continue (Y/N)?");

This way, the programmer can just type ASK_PROMPT in the code, which is globally replaced with the printf() statement when the code is compiled. (As a bonus, the programmer can universally fix every prompt simply by modifying the single #define.)

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com