Symbolic Constants
Symbolic Constants:-
A
symbolic constant is name that substitute for a sequence of character that
cannot be changed i.e. constant. Constants in C++ refer to fixed values.
Constant values do not change during the execution of a program. These fixed
values are also called literals.
Constants can be of any of the basic data types like an integer
constant, a floating constant, a character constant, or a string literal. A
constant is used to describe a numerical value, or a character string.
Numerical constants may be expressed as real constants, integer constants or
character constants. The constants are treated just like regular variables
except that their values cannot be modified after their definition. You must
initialize a constant when it is created.
Defining
Symbolic Constants:
There
are two simple ways in C++ to define constants:
1. Using #define
pre-processor: The pre-processor directive #define can create a constant by
specifying its name and value, separated by spaces.
2. Using const keyword: The keyword const can create constant by specifying name of constant and assign its value to it using assignment operator.
Syntax:
#define
constant_name value;
const
datatype constant = value;
Example:
#define
pi 3.14;
Comments
Post a Comment