Explain symbolic constant with example?

A symbolic constant is a meaningful name given to a value that is intended to remain constant throughout a program. Instead of using a literal value (e.g., 3.14) throughout a program, a symbolic constant allows the programmer to use a descriptive name (e.g., PI) that is easier to read and remember.


Symbolic constants are typically defined using the #define preprocessor directive in C and C++. Here's an example:

#define PI 3.14159

This defines a symbolic constant named PI with a value of 3.14159. Once defined, PI can be used throughout the program wherever the value of pi is needed. For example:


double radius = 5.0;

double circumference = 2 * PI * radius;

Here, PI is used in the calculation of the circumference of a circle with a radius of 5.0.


Symbolic constants are useful because they make the code easier to read and understand, and they also make it easier to change the value of a constant throughout a program. If the value of pi were to change, for example, it would be much easier to update a single #define statement than to find and replace every instance of 3.14159 in the program.
 

Post a Comment

Previous Post Next Post