Symbolic Constant in C

Symbolic Constant in C

Symbolic Constant in C is a name that substitutes for a sequence of characters or a numeric constant, a character constant, or a string constant. When the program is compiled each occurrence of a symbolic constant is replaced by its corresponding character sequence.

A symbolic constant can be defined as a constant that is represented by a name (symbol) in a program. Like a literal constant, a symbolic constant cannot undergo changes. Whenever the constant’s value is needed in the program, the name of the constant is used in the same way as the name of a variable is used to access its value.

There are two methods for defining a symbolic constant:

      • Using the #define directive
      • Using the const keyword

 

We know that PI is a constant with a value of 3.1416 and it is defined in C by using the const keyword in the following way:

Example

const float PI = 3.1416;         /* defining PI as a constant */

 

Here, since we’ve defined PI as a const, any subsequent attempts to write or modify the value of PI are not allowed in the entire program.

const float PI = 3.1416;
PI = 300;                     /* This is an error. const cannot be manipulated. */

 

The following code segment illustrates the use of symbolic constant (defined by using #define directive) in a C program:

#define PI 3.1416                /* PI has been defined as a symbolic constant in this line */

 

Program

The following program shows the use of symbolic constant to compute the area of a circle

#include<stdio.h>
#define PI 3.1416
int main() {
float area, radius;
printf("Enter the radius of the circle:");
scanf("%f", &radius);
area = PI * radius * radius;
printf("\n The area of the circle with radius %f is: %f", radius, area);
return 0;
}

 

Output

Enter the radius of the circle:3
The area of the circle with radius 3.000000 is:29.608951

 

Also, read the Array in C language

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *