Symbolic constant in C language

Symbolic constant is a name that substitutes for a sequence of characters which may be numeric constant , character constant or a string constant.


When a program is compiled each occurrence of a symbolic constant is replaced by its corresponding character sequence.

Symbolic constants are usually defined at the beginning of program just after header files.


Syntax for symbolic constant

#define name text

#define PI 3.1415

#define LETTER 'y'

#define NAME "john"

Note : Character is enclosed in single quote 'y'  and String ( character array ) is enclosed in double quote  "john".


Where name represents a symbolic name , usually written in uppercase letters and text represents sequence of character and  doesn't end with semicolon . 

if semicolon is added then compiler treat it as a part of text

like #define PI 3.14;

compiler replaces all "PI"  with "3.14;" during compilation so never put semicolon at the end of text.



After definition of symbolic constant at the beginning of program you can use it letter like below

area = PI * radius * radius;


Advantages of symbolic constant in c

#define feature which is used to define symbolic constant is feature of C preprocessor this means it is processed before compiling of actual program  and also if we have certain constants used multiple times then we can change all value with single change 

Example

#define TAXRATE 0.15

tax rate can be used many times,  lets say 10 or 20 times  but its very hard for us to change value at 20 place so its good to use symbolic constant there so that we can change it in single step.


 



Contact Form

Name

Email *

Message *