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.
Constants are like regular variables, except that the value of a constant cannot be modified after definition.
Integer constants
Integer constants can be decimal, octal, or hexadecimal constants. The prefix specifies the base: 0x or 0X indicates hexadecimal, 0 indicates octal, and no prefix means decimal by default.
Integer constants can also have a suffix, which is a combination of U and L. U means unsigned integer, and L means long integer. The suffix can be uppercase or lowercase, and the order of U and L is arbitrary.
The following are some examples of integer constants:
The following are examples of integer constants of various types:
Floating-point constants
Floating-point constants consist of an integer part, a decimal point, a fractional part, and an exponent part. You can use decimal form or exponential form to represent floating-point constants.
When using decimal form, you must include the integer part, the fractional part, or both. When using exponential form, you must include the decimal point, the exponent, or both. A signed exponent is introduced by e or E.
The following are some examples of floating-point constants:
Boolean constants
There are two boolean constants, both of which are standard C++ keywords:
- trueThe value represents true.
- falseThe value represents false.
We should not regard the value of true as 1 or the value of false as 0.
Character constants
Character constants are enclosed in single quotes. If a constant begins with L (only when uppercase), it indicates that it is a wide-character constant (e.g., L'x'), and it must be stored inwchar_ta variable of type. Otherwise, it is a narrow-character constant (e.g., 'x'), and it can be stored incharin a simple variable of that 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 specific characters have special meanings when preceded by a backslash, and are used to represent things like newline (\n) or tab (\t). The following table lists some of these escape sequence codes:
| Escape sequences | Meaning |
|---|---|
| \\ | \ character |
| \' | ' character |
| \" | " character |
| \? | ? character |
| \a | Alarm bell |
| \b | Backspace |
| \f | Form feed |
| \n | Newline |
| \r | Carriage return |
| \t | Horizontal tab |
| \v | Vertical tab |
| \ooo | Octal numbers with one to three digits |
| \xhh . . . | Hexadecimal numbers with one or more digits |
The following example shows some escape sequence characters:
Example
When the above code is compiled and executed, it produces the following results:
Hello World
String constants
String literals or constants are enclosed in double quotes""A string contains characters similar to character constants: ordinary characters, escape sequences, and universal characters.
You can use\as delimiters to break a very long string constant into multiple lines.
The following example shows some string constants:
Example
#include <string>
using namespace std;
int main() {
string greeting = "hello, example";
cout << greeting;
cout << "\n"; // newline
string greeting2 = "hello, \
example";
cout << greeting2;
return 0;
}
hello, example hello, example
Define constants
In C++, there are two simple ways to define constants:
- Usage#definePreprocessor.
- UsageconstKeyword.
#define preprocessor
The following is the form of defining a constant using the #define preprocessor:
#define identifier value
See the examples below for details:
Example
When the above code is compiled and executed, it produces the following results:
50
const keyword
You can useconstThe prefix declaration specifies a constant of a specific type, as shown below:
const type variable = value;
See the examples below for details:
Example
When the above code is compiled and executed, it produces the following results:
50
Please note that defining constants in uppercase letters is a good programming practice.
other extensions