C++ Constants
Constants are fixed values that do not change during program execution. These fixed values are also calledliterals.
Constants can be of any basic data type and can be divided into integer numbers, floating-point numbers, characters, strings, and Boolean values.
A constant is like a regular variable, except its value cannot be modified after definition.
Integer Constants
An integer constant can be decimal, octal, or hexadecimal constants. A prefix specifies the base: 0x or 0X means hexadecimal, 0 means octal, and no prefix means decimal by default.
An integer constant may also carry a suffix that is a combination of U and L, where U means unsigned integer and L means long integer. The suffix may be uppercase or lowercase, and the order of U and L is arbitrary.
Here are a few examples of integer constants:
212
// Valid
215
u
// Valid
0xFee
L
// Valid
078
// Invalid: 8 is not an octal digit
032
UU
// Invalid: suffix cannot be repeated
The following are examples of various types of integer constants:
85
// Decimal
0213
// Octal
0x4b
// Hexadecimal
30
// Integer
30
u
// Unsigned integer
30
l
// Long integer
30
ul
// Unsigned long integer
Floating-point Constants
A floating-point constant consists of an integer part, a decimal point, a fractional part, and an exponent part. You can represent a floating-point constant using either a decimal form or an exponent form.
When using the decimal form, you must include the integer part, the fractional part, or both. When using the exponent form, you must include the decimal point, the exponent, or both. A signed exponent is introduced by e or E.
Here are a few examples of floating-point constants:
3
.14159
// Valid
314159
E
5
L
// Valid
510
E
// Invalid: incomplete exponent
210
f
// Invalid: no decimal or exponent
.
e55
// Invalid: missing integer or fraction
Boolean Constants
There are exactly two Boolean constants, and they are both standard C++ keywords:
- true represents true.
- false represents false.
We should not treat the value of true as 1 and the value of false as 0.
Character Constants
A character constant is enclosed in single quotes. If the constant begins with L (only when uppercase), it denotes a wide-character constant (e.g., L'x'), which must then be stored in a wchar_t type variable. Otherwise, it is a narrow-character constant (e.g., 'x'), which can be stored in a char simple variable of type.
A character constant can be an ordinary character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0').
In C++, certain characters have special meaning when preceded by a backslash, and are used to represent things like a newline (\n) or a tab (\t). The table below lists some such escape sequence codes:
| Escape sequences | Meaning |
|---|---|
| \ | \ character |
| ' | ' character |
| " | " character |
| ? | ? character |
| \a | Alert (bell) |
| \b | Backspace |
| \f | Form feed |
| \n | Newline |
| \r | Carriage return |
| \t | Horizontal tab |
| \v | Vertical tab |
| \ooo | One to three octal digits |
| \xhh . . . | One or more hexadecimal digits |
The example below shows some escape sequence characters:
Example
#include
<
iostream
using
namespace
std;
int
main
(
)
{
cout
<<
"
Hello
\
tWorld
\
n
\
n
"
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
Hello World
String Constants
A string literal or constant is enclosed in double quotes "". A string contains characters similar to character constants: ordinary characters, escape sequences, and universal characters.
You can use \ as a line separator to break a long string constant across lines.
The example below shows some string constants:
Example
#include
#include
using
namespace
std
;
int
main
(
)
{
string greeting
=
"hello, runoob"
;
cout
<<
greeting
;
cout
<<
"\n"
;
// Newline
string greeting2
=
"hello, \
runoob"
;
cout
<<
greeting2
;
return
0
;
}
hello, runoob
hello, runoob
Defining Constants
In C++, there are two simple ways to define constants:
- Use #define The preprocessor.
- Use const The const keyword.
#define Preprocessor
The following is the form for defining constants using the #define preprocessor:
#define identifier value
See the example below:
Example
#include
<
iostream
using
namespace
std;
#define
LENGTH
10
#define
WIDTH
5
#define
NEWLINE
'\
n
'
int
main
(
)
{
int
area
;
area
=
LENGTH
WIDTH
;
cout
<<
area
;
cout
<<
NEWLINE
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
50
const Keyword
You can use the const prefix to declare a constant of a specified type, as shown below:
const type variable = value;
See the example below:
Example
#include
<
iostream
using
namespace
std;
int
main
(
)
{
const
int
LENGTH
=
10
;
const
int
WIDTH
=
5
;
const
char
NEWLINE
= '\
n
';
int
area
;
area
=
LENGTH
WIDTH
;
cout
<<
area
;
cout
<<
NEWLINE
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
50
Note that defining constants in uppercase letters is a good programming practice.