A symbolic constant is a symbol, often similar to a variable name, which represents one and only one value. You can think of them as variables that never change (not while the program is running anyway). Often times in a program you might want to use a single value for something over and over. Well, that's what symbolic constants are for. Before I go on, let's see how to make a symbolic constant.
HTML Code:
#include <stdio.h>
#define PI 3.14
int main()
.
.
First you type #define then a symbol, then the value it represents. Although it is not required, C programmers usually make the symbol all uppercase. Never end #define statements with a semicolon. Symbolic constants should always be defined
above the main function. That's why I included that other stuff, so you could see where they go. I usually try to put them right after the #include things. Basically, this tells the compiler to use the value 3.14 every time it sees the symbol PI. Now, you can use PI all through your program instead of 3.14. Why bother? Well, let's say you wrote a big math program, and you use PI a dozen times. Then you decided that you needed more precision; instead of 3.14 you want 3.14159. If you used #define to make a constant, then all you have to do is change the value once. If you actually typed in 3.14 all the time, you will need to change it a dozen times.
In our guessing game in the section on functions we probably should have used a constant for our range. Although we only needed it once anyway, #defines are usually easier to find than hunting through a program. As a rule of thumb, if a value is not going to change throughout your program, it should be a symbolic constant.
Here is the guessing game program revised with a constant.
HTML Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define RANGE 10
//function prototypes//
void seedrnd();
int rnd( int );
int main()
{
int guess, num;
/* function call to rnd which will return a random number between 1 and 10*/
num = rnd( RANGE );
printf("I have chosen a number between 1 and 10!\n");
printf("Enter guess: ");
scanf("%d", &guess);
if( guess == num )
printf("You guessed it! Bully for you!\n");
else
printf("I'm sorry, the answer is %d\n", num);
printf("Please play again\n");
return 0;
}
// function definitions //
void seedrnd()
{
// Used to set up the random number generator //
srand( time(NULL) );
}
int rnd( int range)
{
int num;
/* call to seedrnd */
seedrnd();
/* The % thing divides the numbers, but gives us the remainder
of the division, instead of the answer */
num = rand() % range; //rand() gets a random number
num++; //Add one to get rind of zeros
return num;
}
Fairly simple. When rnd is called, the value ten will be passed to it. If we change the #define line to this:
#define RANGE 100
Then when rnd is called the value 100 will be passed. Actually, we don't need to pass any parameters at all in this program. Symbolic constants are usable by all functions, not just main. So, in the rnd function we could have used the constant RANGE after the modulus operator. Constants are not limited to numeric values. If you want a constant string you can do that too. It would look like this:
#define ADVICE "Stop, drop, and roll"
You might use it like this: puts(ADVICE); or any other place you would use a string.
TECHNICAL NOTES
*Remember, anything with a # in front of it is called a preprocessor directive.
*Before a program is actually compiled, this thing called a preprocessor finds all the symbols you defined and replaces the text in your file with the values they represent.
*The expression "rule of thumb" comes from the ruling of an 1800's judge who said it was ok for a man to beat his wife as long as the stick he used was no wider than his thumb.