# Constants In C++: Literal Constant, Symbolic Constant, Const Qualifier, Define Directive ## Constants:

Constants in C++- The quantities that cannot change their value during the execution of the program are called constants.

The constants in C++ are divided into two basic types:

• Literal Constant in C++
• Symbolic Constant in C++

### Literal Constant in C++:

The word ‘literal ‘ means exact or accurate. A literal is a constant in c++ having an independent value that is used in a program source code. For example, the values 10, 10.3, and “Excellent” represent literal constants in C++.

Literal constants in c++ are further divided into:

• Integer constants in C++
• Floating-point constants
• Character constants
• String constants

#### Integer Constants in C++:

the numeric value that has no decimal part is known as integer constants. The numeric value 10, -16, 340, 89 are example of integer constants in c++. The integer constants in c++ must lie within the range of integer. The + and – signs can also be used with integer constants. Usually, the integer constants are used in an arithmetic expression.

#### Floating-point Constants in C++:

The numeric values that have decimal parts are known as float-point constants. They may be positive or negative. The numeric values 1.544, 4.0, and -6.2 are examples of floating-point constants in c++.

The number 7.619F is also an example of floating-point constants. The letter F in the number indicates that its data type is float. If “F” is omitted then the data type of number will be “double”. We can also specify the data type as “long double” using the letter L. for example, the number 21.63L indicates the long double floating-point constants in c++.

We can also write floating-point constants using exponential notation. Usually, large values are written in exponential notation. For example, 70000000.0 can be written as 70.0E6 in exponential notation. Similarly, 16.72 x 1019 can be written as 16.72E19. the letter E represents the exponent.

#### Character Constants in C++:

Character constants in C++ always enclosed in single quotation marks. A single character may be an alphabet, digit, or special character. For example, ‘x’, ‘y’, ‘+’, ‘a’, and ‘9’ represent character constants in C++.

#### String Constants in c++:

A sequence of characters enclosed in double quotation marks is known as string constant or string literal. The c++ compiler considers a string constant in c++ as a single token. For example, “my First program”, “012345”, BCS-999” represent string constants.

### Symbolic Constants in C++:

The symbolic constant is a constant identifier. It must be initialized. After a symbolic constant is initialized, its value cannot be changed during program execution. In C++ symbolic constants can be declared in two ways:

• Using ‘const’ keyword
• Using ‘define’ directive

#### The ‘const’ Keyword in c++:

The ‘const’ keyword is used to declare a constant identifier. The constant identifier is declared in a similar way as a variable is declared except that a keyword “const” is used to specify a constant identifier. A value must be assigned to a constant identifier at the time of its declaration. Once a value is assigned, it cannot be changed during program execution. For example, to declare a variable PI and to assign value 3.1417 by using ‘const’ keyword, the statement is written as:

const float PI=3.1417;

##### Example write a program that computes the volume of a cylinder using a const qualifier. The formula to find the volume to the cylinder is: volume=πR^2 xH. The value of π is 3.1417: #### The ‘define’ Directive:

The ‘define’ is a preprocessor directive. It is used to define a constant identifier known as a constant macro. A constant macro is an identifier, which is assigned a particular constant in c++ value. like other preprocessor directives, the define directive is also used at the beginning of the program.

The general syntax of ‘define’ directive is as follows:

#define identifier expression;

Where

Identifier:

It indicates the name of a constant identifier to which the constant value is to be assigned. This name cannot be used again in the program as a variable name or function name etc.

Expression:

It indicates the constant value that is to be assigned to the identifier. It may be a constant value, a string, or a string or an arithmetic expression.

In the following statement, PI is assigned value 3.141592 and ‘city’ is assigned value ‘uk’.

#define PI 3.141593

#define city uk

##### Example write a program that computes the area of a circle using constant define directive. The formula to find the area of a circle is area=πR^2. The value of π is 3.1417. ## Difference Between define and const in c++:

The difference between the ’define’ directive and ‘const’ qualifier is as follows:

 Define Directive const Qualifier It is used as a preprocessor directive. It is used as a statement. It is not terminated with a semicolon (;) It is terminated with a semicolon. The data type of constant identifier is not specified The data type of constant identifier is specified.

## Related Article:

Friend Function And Friend Class In C++ With Examples

### Recommended For You 