C++ constants
Constants are fixed values that do not change during program execution. These fixed values are also called literals.
Constant can be any basic data type, which 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 it is defined.
Integer constants
Integer constants can be decimal, octal or hexadecimal constants. The prefix specifies the base: 0x or 0X for hexadecimal, 0 for octal, and without a prefix the default is decimal.
Integer constants can also have a suffix. The suffix is a combination of U and L. U represents an unsigned integer (unsigned) and L represents a long integer (long). The suffix can be uppercase or lowercase, and U and L can be in any order.
The following are examples of several integer constants:
212 // 合法的 215u // 合法的 0xFeeL // 合法的 078 // 非法的:8 不是八进制的数字 032UU // 非法的:不能重复后缀
The following are examples of various types of integer constants:
85 // 十进制 0213 // 八进制 0x4b // 十六进制 30 // 整数 30u // 无符号整数 30l // 长整数 30ul // 无符号长整数
Floating point constants
Floating Point constants consist of an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating-point constants in decimal or exponential form.
When expressed in decimal form, the decimal point, exponent, or both must be included. When expressed in exponential form, an integer part, a decimal part, or both must be included. Signed exponents are introduced with e or E.
The following are several examples of floating point constants:
3.14159 // 合法的 314159E-5L // 合法的 510E // 非法的:不完整的指数 210f // 非法的:没有小数或指数 .e55 // 非法的:缺少整数或分数
Boolean constants
There are two Boolean constants, both of which are standard C++ keywords:
true The value represents true.
#false The value represents false.
We should not treat the value of true as 1 and 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 means that it is a wide character constant (such as L'x'), in which case it must be stored in a variable of type wchar_t. Otherwise, it is a narrow character constant (such as 'x'), in which case it can be stored in a simple variable of type char.
Character constant can be an ordinary character (such as 'x'), an escape sequence (such as '\t'), or a universal character (such as '\u02C0').
In C++, there are some specific characters. When they are preceded by a backslash, they have special meanings and are used to represent newlines (\n) or tabs (\t )wait. The following table lists some such escape sequence codes:
#Escape sequence | Meaning |
---|---|
\\ | \ characters |
\' | ' characters |
\" | " Characters |
\? | ? Characters |
\a | Alarm ringtone |
\b | Backspace key |
\f | Form feed |
\n | Line break |
\r | Enter |
\t | Horizontal tab |
Vertical tab | |
One to three digits in octal number | |
One or more digits in hexadecimal number |