C++ Constants
C++ constants is a quantity that cannot be changed during program execution. Following are two types of constants in C++:
- Literal constant
- Symbolic constant
1) 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
The following statement contains string literal constant Hello Zitoc:
cout<<"Hello Zitoc";
similarly, in the following statement:
int roll_number = 15;
roll_number is integer variable and 15 is a literal constant.
Types of Literal Constants
Different types of literal constants are as following:
- Integer constant
- Floating-point constant
- Character constant
- String constant
Integer constant
Integer constants are numeric values without a fraction or decimal point. Both positive and negative integer constant is used. The minus sign – is used for negative integer constants. If no sign used, the value is positive by default.
Example
Some examples of integer constants are as following:
45 32 23 -34 -21
The use of L at the end of integer indicates that is a long integer
56000L -65000L
Floating Point Constant
Floating-point constants are numeric values with a fraction or decimal point. Both positive and negative floating-point constants. If no sign is used, the value is positive by default. The use of F, of, f at the end of the real value indicates that the value is a, float.
Example
Some examples of floating-point constants are as following:
75.50F 22.10f
If F or f is not used, the value considered as double.
32.15 42.79
The use of L at the end of the real value indicates that it is a long double.
530000.22L
Character Constants
Any character written in single quotes is known as a character constant. All alphabetic characters, digits and special symbols can be used as character constants.
Example
Some example of character constants are the following:
‘A’ ‘n’ ‘9’ ‘=’ ‘$’
String Constants
A set of characters written in the double quotation is called string or string constant. It may constant of any alphabetic characters, digits and special symbols.
Example
Some example of string constants are the following:
“Pakistan” “126” “9-Road Layyah”
2) Symbolic Constants
A symbol 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 the 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 used declared in two ways.
- const Qualifier
- define directive
- const Qualifier
a const qualifier is used to define a constant. The constant is declared by specifying its name and data type. The constant must be initialized value cannot be changed during program 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 a constant declaration is as follows:
const int k = 143;
The above example defines a constant k that is initialized with 143. The value 143 cannot be changed during the execution of the program.