Home | Computer | What Are Constants In C/C++?-Literal Constant, Symbolic Constant

What Are Constants In C/C++?-Literal Constant, Symbolic Constant

September 4, 2022

Constant is a quantity whose value can only be determined once at compile time. There are two types of constants in C/C++.

Literal Constant

A literal constant is a value that is typed directly in a program. It appears in the program wherever it is needed.

Example

Cout<<“Hello World”;

The following statement contains a string literal constant Hello World.

Similarly, in the following statements:

Int age = 19;

Age is an integer variable and 19 is a literal constant,

Types of Literal Constants

Different types of literal constants are as follows:

Integer Constant

Integer constants are numeric values without fractions or decimal places. Both positive and negative integers are used. The minus symbol – is used for negative numbers. If no sign is given, the number is assumed to be positive.

Example

Some examples of integer constant are as follows:

87    45   -10    -5

Floating Point Constants

Floating Point constants are numeric values with fractions or decimal points. Both the positive and negative floating-point constants are used. The minus sign – Is used for negative floating-point constants. If no sign is used, the value is positive by default. The use of F at the end of the real value indicates that the value is float.

Examples

Some examples of floating-point constants are as follows;

50.75F           10.22F

If F or f is not used, the value is considered as double.

15.32    42.79

Character Constants

Any character written in single quotes is known as a character constant. All alphabetic characters, digital, and special symbols can be used as a character constant.

Example

Some examples of character constants are as follows:

‘A’   ‘n’    ‘=’    ‘$’

String Constant

A set of characters written in double quotations is called a string or a string constant. It may consist of any alphabetic characters, digits, and special symbols.

Example

Some examples of string constants are as follows:

“Pakistan”        “123”        “99-Mall Road, Lahore”

Symbolic Constants

A symbolic constant is a name given to values that cannot be changed. A constant must be initialized. After a constant is initialized, its value cannot be changed. Symbolic constants are used to represent a value that is frequently used in a program.

A symbolic constant PI can be used to indicate the value of Pi which is 3.141593. PI can be written in the program wherever its value is required.

Symbolic constants can be declared in two ways:

Const Qualifier

Define Directive

Const Qualifier

Const Qualifier is used to define a constant. The constant is declared by specifying its name and data type. The constant must be initialized with some value. The initialized value cannot be changed during execution.

Syntax

The syntax of declaring constants with const qualifier is as follows:

Const data-type identifier = value;

Const: It is a keyword used to define a constant.

Data type: It indicates the data type of the constant.

Identifier:  It represents the name of the constant.

Value: It represents the value with which the constant is initialized.

Example

An example of constant declaration is as follows:

Const int N = 100;

The above example defines a constant N that is initialized with 100. The value 100 cannot be changed during the execution of the program.

Define Directive

A Define directive is also used to define a constant. The difference between const qualifier and define directive is that define directive does not specify the data type of the constant. Define directive starts with the symbol #. It is not determined with a semicolon.

#define identifier value

#:   it indicates the start of the preprocessor directive

Define: It is used to define a constant

Identifier: It is the name of the constant

Value: It represents the value associated with the identifier.

The preprocessor directive replaces all occurrences of the identifier with the value. The identifier is conventionally written in uppercase.

Example

# define PI 3.1411596

Frequently Asked Questions(FAQs)

What are constants and their types?

Constant is a quantity whose value can only be determined once at compile time. It is not possible to change the value of a constant during the execution of the program.
There are two types of constant
·         Literal Constant
·         Symbolic Constant

Why is constant used in C++?

In C/C++, the const keyword specifies that a variable has a static value. This keyword tells the compiler to prevent programmers from modifying it. Constant values default to being external in C/C++, so they can only appear in source files.

What is a literal constant?

A literal constant is a value, such as a dollar amount, that is typed directly into your program. The programmer inserts the constant wherever it is needed in the program.

What are the three literal constants used in C/C++?

·         Integers Constant
·         Floating Point Constant
·         Character Constant

What is the advantage of a symbolic constant?

The advantages of symbolic constants are clear: they make code easier to read, which means it’s easier for someone to understand it. Compilers flag unintentional changes to variables with symbolic constants.