learnprogramming123

LEARN PROGRAMMING 123
your resources to programming and free tutition



Navigation

 
C
 
C++ soon here!
VB - soon here!

Search

 

 

 

 

Next Back

 

In this lesson you will learn how to:

  • Declare constants
  • Use pre-processor directives

Q. What does a blonde say after having sex ?
A. What team do you guys play for !

Pre-processor statements

    The define statement is used to make programs more readable. Consider the following examples:

#define TRUE 1
#define FALSE 0
#define NULL 0
#define AND &
#define OR |
#define EQUALS ==

game_over = TRUE;
while (list_pointer != NULL)

Note that pre-processor directives begin with a # symbol, and are NOT terminated by a semi-colon. Traditionally, pre-processor statements are listed at the beginning of the source file.

Pre-processor statements are handled by the compiler (or pre-processor) before the program is actually compiled. All # statements are processed first, and the symbols (like TRUE) which occur in the C program are replaced by their value (like 1). Once this substitution has taken place by the pre-processor, the program is then compiled. In general, pre-processor constants are written in upper case.

Literal substitution of symbolic constants using #define

Lets now examine a few examples of using these symbolic constants in our programs. Consider the following program which defines a constant called TAX_RATE.

#include <stdio.h>

#define TAX_RATE 0.10

int main(void)
{

    flat balance;
    float tax;

    balance = 72.10;
    tax = balance * TAX_RATE;
    printf(“The tax on %.2f is %.2f\n”, balance, tax);

    return 0;

}

The pre-processor first replaces all symbolic constants before the program is compiled, so after pre-processing the file (and before it’s compiled), it now looks like:

#include <stdio.h>

#define TAX_RATE 0.10

int main(void)
{

    float balance;
    float tax;

    balance = 72.10;
    tax = balance = 0.10;
    printf(“The tax on %.2f is %.2f\n”, balance, tax);

    return 0;

}

You cannot assign values to the symbolic constants.

Consider the above program as an example, look at the changes we have made below. We have added a statement which tries to change the TAX_RATE to a new value.

#include <stdio.h>

#define TAX_RATE 0.10

int main(void)
{

    float balance;
    float tax;

    balance = 72.10;
    TAX_RATE = 0.15;
    Tax = balance * TAX_RATE;
    printf(“The tax on %.2f is %.2f\n”, balance, tax);

    return 0;

}

This is illegal. You cannot re-assign a new value to a symbolic constant.

Its literal substitution, so beware of errors

As shown above, the pre-processor performs literal substitution of symbolic constants. Lets modify the previous program slightly, and introduce an error to highlight a problem.

#include <stdio.h>

#define TAX_RATE 0.10;

int main(void)
{

    float balance;
    float tax;

    balance = 72.10;
    tax = (balance * TAX_RATE) + 10.02;
    printf(“The tax on %.2f is %.2f\n”, balance, tax);

    return 0;

}

In this case, the error has been introduced is that the #define is terminated with a semi-colon. The pre-processor performs the substitution and the offending line (which is flagged an error by the compiler) looks like

    tax = (balance * 0.10;) + 10.02;

However, you do not see the output of the pre-processor. If you are using TURBO C, you will see only:

    tax = (balance * TAX_RATE) + 10.02;

flagged as an error, and this actually looks okay (but it’s not! After substitution takes place);

Making programs easy to maintain by using #define

The whole point of using #define in your programs is to make them easier to read and modify. Considering the above programs as examples, what changes would you need to make if the TAX_RAE was changed to 20%. Obviously, the answer is once, where the #define statement which declares the symbolic constant and it’s value occurs. You would now change it to read:

    #define TAX_RATE 0.20

Without use of symbolic constants, you would hard code the value 0.20 in your program, and this might occur several times (or tens of times). This would make changes difficult, because you would need to search and replace every occurrence in the program. However, as the programs get larger, what would happen if you actually used the value 0.20 in a calculation that had nothing to do with the TAX_RATE!

back to top          exercises       esson 4         lesson 6     view summary


 






1