SYMBOLIC CONSTANTS


SYMBOLIC CONSTANTS

Symbolic constants allows the programmer to define a name for a constant and use that name throughout the program.  Unlike variables, its value remain fixed throughout the execution of a program i.e.  they remain constant.  In C, symbolic constants can be defined using the following methods,
·         using #define preprocessor directive
·         using const qualifier
·         using enum facility
We shall  discuss #define preprocessor directive and const qualifier here because  enum is a type of data that is already I have discussed in data type.  

#define DIRECTIVE

One of the method of defining symbolic constant is using #define preprocessor directive.  It takes the following form,
#define SYMBOLIC_NAME constant_value
when a symbolic constant is defined in this way in a program, all subsequent occurrences of SYMBOLIC_NAME will be replaced with constant_value by the preprocessor before the program is compiled.  For example,
#define PI 3.14159  
replace every occurrence of symbolic name PI with a value 3.14159 before program compilation. The preprocessor does not evaluate the code in any way rather it does a simple text substitution. 
One of the advantage of using symbolic constant is easier program maintenance.  If the program is modified in near future and value of symbolic name needs to be changed then only the changes need to be made in #define directive.  Now when the program is recompiled, all occurrences of constant will be modified.
 while defining symbolic constants using #define directive, the following rules should be constant will be followed,
 1. It is preferred to write symbolic name in uppercase letters so as to distinguish it from variable names which are written in lowercase letters. 
2 .  The pound symbol (#) is the first character that must appear in #define statement.  Also, there should not be any space between '#' and word 'define'
3.  It is usually specified at the beginning of the program although it can be placed anywhere. 
4.  #define statement must not end with a semicolon. 
5. Only one #define directive statement can be placed in a line. 
6. Once defined, the symbolic name should not be assigned any other value within the program using the assignment statement. 
EXAMPLE:
To calculate the area and circumference of a circle of given diameter.
 #include <stdio.h>
#define PI 3.14159
main ()
{
double diameter = 2.5; 
double radius, area, circumference; 
radius = diameter / 2; 
circumference = 2 * PI * radius;
area = PI * radius * radius; 
printf ("\ nThe circumference =" 16 ", circumference);
printf (" \ nArea =% lf ", area);
getch();
}
 OUTPUT:
The circumference = 7.853975
Area = 4.908734

const qualifier:

 Another way of defining symbolic constant is using ‘const’ qualifier.  They are declared in a manner similar to variables except that declarations are preceded by qualifier 'const' and must be initialized. Once a value is assigned to a constant, it cannot be changed. If an attempt is made to change its value, a compile  time error will be triggered. It takes the following form.
const type identifier = value;
For instance,
const int MONTHS = 12;
this declaration defines a constant MONTHS of type int that contains a value 12. It is just a read only value.
It is preferred to use ‘const’ qualifier for declaring symbolic constant instead of #define preprocessor directive because the preprocessor does not performed does not perform any type checking of symbolic name specified in #define directive. However, when defining constant with const, as the data type is exclusively specified so the compiler can enforce that it is used according to its type.

No comments

Powered by Blogger.