In this tutorial you will learn about the C++ Constants and its application with practical example.
C++ Constants
Constants refers to immutable values. Constants are basically literals whose values cannot be modified or changed during the execution of program. Constant are also called as Literals. Constant must be initialized when declared as values cannot be assigned it to later. In C++, constants can be of following four basic types –
Integer Constants
An integer constant can only hold integer quantity which is a sequence of whole numbers or digits.
- Integer constant can not have a decimal point or fractional part.
- Blanks and commas are not allowed within an integer constant.
- An integer constant can be either +ve or -ve.
- The constant must lie within the range of the declared data type (including qualifiers long,short etc.).
An integer constant can be either Decimal, Hexa Decimal or Octal. A decimal integer constant consists of any combination of digits taken from the set 0 through 9. If the decimal constant contains two or more digits, the first digit can not be 0 (Zero).
Example:-
1 |
12, 15, 456 etc. |
An octal integer constant is a combination of digits taken from the set 0 through 7. In octal representation first digit must be a 0(Zero), in order to identify the constant as an octal number.
Example:-
1 |
0, 05, 054 |
A hexadecimal integer constant must begin with either 0x or 0X. It can then be followed by any combination of digits taken from the set 0 through 9 and alphabets from A to F (either upper-case or lower-case are valid).
Example:-
1 |
0x65F, 0X7A |
Floating Point Constants
Floating point or real constants contains a decimal point or an exponent.
- A real constant must have at least one digit each to the left and right of the decimal point.
- It can be either +ve or -ve.
- Commas and blank space are not allowed within a real constant.
A real constant can be represented in two forms: Factorial form or Exponential form. A real constant in a fractional form must have at least one digit each to the left and right of the decimal point.
Example:-
1 |
15.75, -2.25 |
A floating-point in exponent form consists of a mantissa and an exponent.
Example:-
1 |
-2.5e-3, 25E-4 |
Character Constants
A character constant is a single character , enclosed in single quotation marks. It can be a single alphabet, a digit or a special symbol.Maximum length of a character constant is one character.
Example:-
1 |
'A', '5', '$' |
String Constants
A string constant is sequence of characters enclosed in double quotes.It may contain letters, digits, special characters and blank space.
Example:-
1 |
"HELLO","world" |
Defining Constants In C++
In C++, constants can be created in following two ways –
- Using #define preprocessor.
- Using const keyword.
Note:- By convention constant names are always uppercase.
Define Constants Using #define Preprocessor
In C++, constants can be defined using #define preprocessor as following –
Syntax:-
1 |
#define constName constValue |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> using namespace std; #define PI 3.141 #define NEWLINE '\n' int main(){ float radius, area; cout <<"W3Adda - C++ Constants"<<NEWLINE; cout <<"Enter radius of circle"<<NEWLINE; cin >> radius; // Area of Circle = PI x Radius X Radius area = PI*radius*radius; cout <<"Area of circle : " << area; return 0; } |
Output:-
Define Constants Using const Keyword
In C++, constants can be defined using const as following –
Syntax:-
1 |
const type constName= constValue; |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> using namespace std; int main(){ const float PI =3.141; const char NEWLINE ='\n'; float radius, area; cout <<"W3Adda - C++ Constants"<<NEWLINE; cout <<"Enter radius of circle"<<NEWLINE; cin >> radius; // Area of Circle = PI x Radius X Radius area = PI*radius*radius; cout <<"Area of circle : " << area; return 0; } |
Output:-