Cplusplus 简明教程

C++ Constants/Literals

常量指程序可能不会更改的固定值,它们被称为 literals

Constants refer to fixed values that the program may not alter and they are called literals.

常量可以是任何基本数据类型,可以分为整数、浮点数、字符、字符串和布尔值。

Constants can be of any of the basic data types and can be divided into Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.

同样,常量与普通变量一样,只是它们的数值在定义后不可修改。

Again, constants are treated just like regular variables except that their values cannot be modified after their definition.

Integer Literals

整数常量可以是十进制、八进制或十六进制常量。前缀指定基数或基数:十六进制的 0x 或 0X、八进制的 0,不指定基数则表示十进制。

An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.

整数文本还可以有一个后缀,它是 U 和 L 的组合,分别代表无符号和长。后缀可以是大写或小写,并且可以按任何顺序排列。

An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.

以下是整数文本的一些示例:

Here are some examples of integer literals −

212         // Legal
215u        // Legal
0xFeeL      // Legal
078         // Illegal: 8 is not an octal digit
032UU       // Illegal: cannot repeat a suffix

以下是一些不同类型整数文本的其他示例:

Following are other examples of various types of Integer literals −

85         // decimal
0213       // octal
0x4b       // hexadecimal
30         // int
30u        // unsigned int
30l        // long
30ul       // unsigned long

Floating-point Literals

浮点文本具有整数部分、小数点、小数部分和指数部分。你可以以十进制形式或指数形式表示浮点数文本。

A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form.

在使用十进制形式表示时,必须同时包含小数点和指数,而在使用指数形式表示时,必须包含整数部分和小数部分。带符号的指数由 e 或 E 引入。

While representing using decimal form, you must include the decimal point, the exponent, or both and while representing using exponential form, you must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E.

以下是浮点数文本的一些示例:

Here are some examples of floating-point literals −

3.14159       // Legal
314159E-5L    // Legal
510E          // Illegal: incomplete exponent
210f          // Illegal: no decimal or exponent
.e55          // Illegal: missing integer or fraction

Boolean Literals

有两个布尔文本,它们是标准 C++ 关键字的一部分:

There are two Boolean literals and they are part of standard C++ keywords −

  1. A value of true representing true.

  2. A value of false representing false.

你不应该认为 true 的值等于 1,而 false 的值等于 0。

You should not consider the value of true equal to 1 and value of false equal to 0.

Character Literals

字符文本用单引号引起来。如果文本以 L(仅大写)开头,则它是一个宽字符文本(例如,L’x'),并且应存储在 wchar_t 类型变量中。否则,它是一个窄字符文本(例如,'x'),并且可以存储在 char 类型的简单变量中。

Character literals are enclosed in single quotes. If the literal begins with L (uppercase only), it is a wide character literal (e.g., L’x') and should be stored in wchar_t type of variable . Otherwise, it is a narrow character literal (e.g., 'x') and can be stored in a simple variable of char type.

字符文本可以是普通字符(例如,'x')、转义序列(例如,'\t')或通用字符(例如,'\u02C0')。

A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0').

C++ 中的某些字符在它们前面加上反斜杠时将具有特殊含义,并且用于表示换行符(\n)或制表符(\t)。在这里,你有这样一些转义序列代码的列表:

There are certain characters in C++ when they are preceded by a backslash they will have special meaning and they are used to represent like newline (\n) or tab (\t). Here, you have a list of some of such escape sequence codes −

Escape sequence

Meaning

|\ character

\'

' character

\"

" character

\?

? character

\a

Alert or bell

\b

Backspace

\f

Form feed

\n

Newline

\r

Carriage return

\t

Horizontal tab

\v

Vertical tab

\ooo

Octal number of one to three digits

\xhh . . .

以下是显示一些转义序列字符的示例:

Following is the example to show a few 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 result −

Hello   World

String Literals

字符串文本用双引号引起来。字符串包含与字符文本类似的字符:普通字符、转义序列和通用字符。

String literals are enclosed in double quotes. A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.

你可以使用字符串文本将长行拆分为多行,并使用空格分隔它们。

You can break a long line into multiple lines using string literals and separate them using whitespaces.

以下是字符串文本的一些示例。所有这三种形式都是相同的字符串。

Here are some examples of string literals. All the three forms are identical strings.

"hello, dear"

"hello, \

dear"

"hello, " "d" "ear"

Defining Constants

在 C++ 中有两种简单的方法来定义常量:

There are two simple ways in C++ to define constants −

  1. Using #define preprocessor.

  2. Using const keyword.

The

以下是使用 #define 预处理程序来定义常量的形式:

Following is the form to use #define preprocessor to define a constant −

#define identifier value

以下示例对此进行了详细说明:

Following example explains it in detail −

#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 result −

50

The const Keyword

您可以使用 const 前缀来声明具有特定类型的常量,如下所示:

You can use const prefix to declare constants with a specific type as follows −

const type variable = value;

以下示例对此进行了详细说明:

Following example explains it in detail −

#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 produces the following result −

50

请注意,以大写字母定义常量是一种良好的编程实践。

Note that it is a good programming practice to define constants in CAPITALS.