C++ Constant
Constant in C++ refers to the values in the program whose value during the entire execution of the program. And these fixed values are called Literals.
These constants may be any of the data types present in C++ such as Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.
They are just like the regular variables, the only difference is that their value cannot be changed once they are defined in the program. The constant is defined by the keyword const
in a program.
Syntax:
1 2 | const float PI = 3.14; |
The constant value cannot be modified later in the program.
1 2 3 | const float PI = 3.14; PI = 3.0 // Compiler Error because PI is a constant. |
Although, there are two ways in which you can define a constant. They are:
- By the use #define preprocessor.
- Another with keyword const itself.
1. #define
: Syntax
1 | <strong><code>#define identifier value</code></strong> |
Example: C++ example for constant.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> using namespace std; //constant #define SIDE 5 int main() { int sq_area; sq_area = SIDE * SIDE ; cout <<"The square area: "<<sq_area; return 0; } //OUTPUT The square area: 25 |
2. constant
: Syntax
1 | <strong><code>const data-type variable_name = value;</code></strong> |
Example: C++ example for constant.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <iostream> using namespace std; int main() { //constant const int side = 5; int sq_area; sq_area = side * side; cout <<"The square area: "<<sq_area; return 0; } //OUTPUT The square area: 25 |
C++ Literals
As mentioned above, literals are the fix value. They are the notation used in a program for fix value.
For example: 1
, 2.5
, 'c'
etc. are the literals because you cannot assign different values to these terms.
The following are the various literals in C++ programming.
1. Integers Literals
An integer literal is a numeric value without any fractional or exponential part. The following are the three integer literal in C++:
- octal (base 8): prefix – 0, starts with
0
. (0, 1, 2, 3, 4, 5, 6, 7) - hexadecimal (base 16): prefix –
0x
or0X
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, A, b, B, c, C, d, D, e, E, f, F) - decimal (base 10): No prefix. (0, 1, 2, 3, 4, 5, 6, 7, 8, 9. )
The Integer Literal may have U or L as a suffix for unsigned and long integers
1 2 3 4 5 6 7 | 22 // decimal 0154 // octal 0x7B // hexadecimal 5 // int 12u // unsigned int 36l // long |
2. Floating-point Literals
Floating-point literals are the numeric value with fractional part or exponential part. The signed exponent is introduced by e or E.
Example:
1 2 | 7.1479 //Decimal Form 23.5e-14 //Exponential Form |
Note: e-14 means 10^-14
3. Boolean Literals
There are two Boolean literals and they are the keyword true
and false
. They are part of standard C++ keywords.
4. character-literal
A character literal refers to the single character enclosed within a single quote.
Example: 'b'
, 'A'
, '5'
, ')'
, '}'
etc.
A character Literal may be an escape sequence that is '\t'
or it can be universal character that is ‘\u02C0’.
Escape Sequence: There are some characters with the special meaning when used with backslash (\
)that cannot be typed but only when used such as the new line ( '\n'
), tab ( '\t'
), etc.
The following are the list of some escape sequence.
Escape sequence | Meaning |
---|---|
\\ | \ character |
\’ | ‘ character |
\” | ” character |
\? | ? character |
\a | Alert or bell |
\b | Backspace |
\f | Form feed |
\n | Newline |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |
5. String Literals
A string literal refers to the sequence of characters enclosed in double quote. A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.
Example:
1 2 3 | "hello World\n" \\string constant with a new line "" \\NULL String constant "x" \\String constant with single character. |
Later on this C++ tutorial, you will learn more on String.