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:

##\vVertical tab\oooOne to three digits in octal number##\xhh . . .

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:

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 spaces as delimiters to separate a long string constant into lines.

The following example shows some string constants. The strings displayed in the following three forms are the same.

"hello, dear"

"hello, \

dear"

"hello, " "d" "ear"

Define constants

In C++, there are two simple ways to define constants:

  • Use #define preprocessor.

  • Use the const keyword.

#define preprocessor

The following is the form of using the #define preprocessor to define constants:

#define identifier value

Please see the following example for details:

#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 above code is compiled and executed, it produces the following results:

50

const Keywords

You can use the const prefix Declare a constant of the specified type as follows:

const type variable = value;

Please see the following example for details:

#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 above code is compiled and executed, it will produce the following results:

50

Please note that it is a good programming practice to define constants in uppercase letters.


#Escape sequenceMeaning
\\\ characters
\'' characters
\"" Characters
\?? Characters
\aAlarm ringtone
\bBackspace key
\fForm feed
\nLine break
\rEnter
\tHorizontal tab
One or more digits in hexadecimal number