Constants are fixed values that do not change during program execution. These fixed values, also known as literal quantities.
Constants can be any of the basic data types and can be grouped into integer numbers, floating-point numbers, characters, strings, and Boolean values.
Constants are like regular variables, except that the values of constants cannot be modified after they are defined.
Integer constants
Integer constants can be decimal, octal, or hexadecimal constants. prefix specifies cardinality: 0x or 0X is hexadecimal, 0 is octal, and decimal is by default without a prefix.
Integer constants can also take a suffix, which is a combination of u and L, u represents an unsigned integer (unsigned), and L represents a long integer. Suffixes can be uppercase or lowercase, and the order of U and L is arbitrary.
Examples of several integer constants are listed below:
212 //valid
215u//valid 0xFeeL//Legal
078 //illegal: 8 not octal number
032UU //illegal: cannot repeat suffix
The following are instances of various types of integer constants:
// decimal
0213 ///eight
0x4b //16// integer
30u //unsigned integer
30l //Long integer
30ul //unsigned long integer
Floating-point constants
A floating-point constant consists of an integer part, a decimal point, a fractional part, and an exponent. You can use decimal form or exponential form to represent floating-point constants.
When you use a decimal representation, you must include a decimal point, an exponent, or both. When used in exponential notation, you must include either an integer part, a decimal part, or both. The signed exponent is introduced by E or E.
Several instances of floating-point constants are listed below:
3.14159 //Legal
314159e-5l//Legal
510E// illegal: Incomplete index
210f //illegal: No decimal or exponent
. E55 // Illegal: Missing an integer or a fraction
Boolean Constants
There are two Boolean constants, all of which are standard C + + keywords:
- A true value represents true.
- The false value represents the false.
We should not treat the value of true as 1, and the value of false as 0.
Word Constants Quantity
The amount of constants is enclosed in single quotes. If the constant starts with L (when uppercase only), it is a wide-constants amount (for example, L ' X '), which must be stored in a variable of type wchar_t. Otherwise, it is a narrow character constants (for example, ' X '), which can be stored in a simple variable of type char.
The constants amount can be an ordinary character (such as ' X '), an escape sequence (such as ' \ t '), or a generic character (such as ' \u02c0 ').
In C + +, there are certain characters that, when they have backslashes in front of them, have special meanings that are used to denote a newline character (\ n) or a tab character (\ t). The following table lists some of these escape sequence codes:
Escape sequence |
Meaning |
\ |
\ character |
\' |
' Character |
\" |
"Character |
\? |
? Character |
\a |
Alarm Ring |
\b |
Backspace key |
\f |
Page breaks |
\ n |
Line feed |
\ r |
Enter |
\ t |
Horizontal tab |
\v |
Vertical tab |
\ooo |
Number of octal in one to three digits |
\xhh ... |
Hexadecimal number of one or more digits |
The following example shows some escape sequence characters:
#include <iostream>
using namespace std;
int main ()
{
cout << "hello\tworld\n\n";
return 0;
}
When the above code is compiled and executed, it produces the following results:
String constants
String literals or constants are enclosed in double quotes "". A string contains characters similar to the character constants: Ordinary characters, escape sequences, and generic characters.
You can use a space as a separator to branch a long string constant.
The following example shows some string constants. The following three forms show the same string.
"Hello, dear" "Hello,
\
Dear"
"Hello," "D" "ear"
Defining constants
In C + +, there are two simple ways to define constants:
- Using #define preprocessor.
- Use the const keyword.
#define Preprocessor
The following is the form of defining constants using the #define preprocessor:
Take a look at the following example:
#include <iostream>
using namespace std;
#define LENGTH
#define WIDTH 5
#define newline ' \ n '
int main ()
{
int area;
Area = LENGTH * WIDTH;
cout << area;
cout << newline;
return 0;
}
When the above code is compiled and executed, it produces the following results:
Copy Code code as follows:
const keyword
You can declare constants of the specified type using the const prefix, as follows:
Const type variable = value;
Take a look at the following example:
#include <iostream>
using namespace std;
int main ()
{
const int LENGTH = ten;
const int WIDTH = 5;
const char newline = ' \ n ';
int area;
Area = LENGTH * WIDTH;
cout << area;
cout << newline;
return 0;
}
When the above code is compiled and executed, it produces the following results:
Copy Code code as follows:
Note that it is a good programming practice to define constants as uppercase letters.
A reference to a constant
If a constant is referenced, the compiler first establishes a temporary variable and then sets the value of the constant into a temporary variable, and the action on that reference is the action on that temporary variable. A C + + constant reference can be initialized with any other reference, but it cannot be changed.
There are two important points to note about the initialization of references:
(1) When the initial value is a left value (can get the address), there is no problem;
(2) When the original value is not a left value, only a const t& (constant reference) value can be assigned. And there is a process for this assignment:
The value is implicitly converted to type T, then the result of the transformation is stored in a temporary object, and the temporary object is used to initialize the reference variable.
Example:
double& dr = 1; Error: Required left value
Const double& CDR = 1;//OK
The actual process of the second sentence is as follows:
Double temp = double (1);
Const double& CDR = temp;
When you make a function argument:
Bc_temp_objects_not_bound_to_nonconst_ref.cpp
//compile with:/EHsc
#include "iostream"
using namespace Std;
Class C {};
void F (C & c) {cout << "c&" << Endl;}
void f (c const & c) {cout << "C Const &" << Endl;}
int main () {
F (C ());
}
Results:
Copy Code code as follows:
More directly, with the basic type:
#include <iostream>
using namespace std;
void display (int const &ref) {cout<<ref<< ' \ n ';}
int main ()
{
int i=1;
Display (i);
int const anotheri=2;
Display (anotheri);
Display (2);
Display (1+2);
Display (static_cast<int> (3.14159));
}
Returns a local object from a function through a C + + constant reference:
It is not correct to normally return a reference to a local object from a function:
T & My_op (void)
{
T;
return t;
} The T object T got destroyed the
returned reference is not valid anymore.
Special case: Returns a constant reference
Const T & my_op (void)
{
T;
return t;
}
Const T & my_t_obj = My_op ();
In this case, the local variable t is not directly destructor, but will be retained until the end of the My_t_obj lifecycle.
In summary, a C + + constant reference syntax can refer to a temporary variable. This method makes sense when using a reference as a function parameter and returning a local variable. I think the constant reference is mainly used as a function parameter or to ensure that the original variable is not modified.