Symbolic constants
Many times, we use special constants in our programs. These constants are repeated several times in a program. The well known constant pie is an example of such constant. Assume that we use pie value 3.14 at many places in our program. Later in order to improve the accuracy, we need to update the pie value from 3.14 to 3.1415. In this case, if we make a mistake to update at any place, the program produces an inaccurate answer. Instead of using directly the constant value at each place, if we define a symbol for such constant once and use the symbol at each place where constant is referred in program, it is more convenient. The symbolic constant is defined in C as follows.
#define SYMBOL constant_name
After defining symbol for a constant, we can use this symbol at every place where constant is used. For example,
#defined PIE 3.14
defines a symbol PIE whose value is 3.14. Now we can use PIE where ever 3.14 is needed as
area = PIE*radius*radius
The main advantage of symbolic constant is that if we want to change the value of constant, we need to change at only one place i.e. at definition. The compiler automatically uses new value at each place.
Normally, the uppercase letters are used for defining symbols to differentiate them from the normal variables. The symbols are also identifiers, but they can not be changed in program. If attempt is made to change it, the compiler will issue an error. The following are some examples of symbolic constants.
#define MAX 10
#define MTS 3.28
Example
Write the program to find area of circle
#include <stdio.h> #define PIE 3.14 void main(){ float radius,area; printf("Enter the radius"); scanf("%f",&f); area = PIE*radius*radius; printf("Area of circle : %f",area); }result
*******************************************
Enter the radius:4.5
Area of circle : 63.584999
******************************************