Cprogramming 简明教程
C - Literals
计算机编程术语中的“文字”是指要分配给变量的值的文本表示。
The term "literal" in computer programming terminology refers to a textual representation of a value to be assigned to a variable.
在 C 中,你可以通过两种方式向一个变量分配一个值 ——
In C, you can assign a value to a variable in two ways −
-
Using literal representation
-
Using an expression
C 中变量的初始化如下进行 ——
The initialization of a variable in C is done as follows −
int x = 10;
另一方面,通过将一个表达式的结果分配给一个变量,对变量进行间接初始化如下所示 ——
On the other hand, an indirect initialization of a variable by assigning it the result of an expression is as follows −
int x = 10;
int y = x*2;
在第一个例子中,10 是一个被分配给“x”的整数字面量。在第二个例子中,“x*2” 表达式的结果被分配给了“y”。
In the first case, 10 is an integer literal assigned to "x". In the second case, the result of "x*2" expression is assigned to "y".
因此,字面量是直接表示在源代码中的某一特定数据类型的值。通常,字面量用于设定一个变量的值。
A literal is thus a value of a certain data type represented directly into the source code. Normally, literals are used to set a value of a variable.
字面量本身不形成任何编程元素。不同的符号用于表示不同数据类型的值。
On their own, literals don’t form any of the programming element. Different notations are used to represent the values of different data types.
Integer Literals in C
在上面的例子中,10 是一个整数字面量。一个不带小数部分、用数字 0 到 9 表示的正整数或负整数是一个 decimal integer literal 。它必须在给定的操作系统平台的可接受范围内。
In the above example, 10 is an integer literal. A positive or negative whole number represented with digits 0 to 9, without a fractional part is a decimal integer literal. It must be within the acceptable range for the given OS platform.
下面的例子将十进制字面量分配给 int 变量 ——
Following examples assign decimal literals to int variables −
int x = 200;
int y = -50;
一个整数字面量还可以有一个后缀,它是“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.
int c = 89U;
long int d = 99998L;
C 允许你用八进制和十六进制数字系统表示一个整数。对于一个八进制的字面量表示,使用 0 作为前缀(确保数字仅使用八进制数字,从 0 到 7)。
C allows you to represent an integer in octal and hexadecimal number systems. For a literal representation of an octal, prefix the number with 0 (ensure that the number uses octal digits only, from 0 to 7).
对于一个十六进制字面量,使用 0x 或 0X 作为前缀。十六进制数字必须有 0 到 9,以及 A 到 F(或 a 到 f)符号。
For a hexadecimal literal, prefix the number with 0x or 0X. The hexadecimal number must have 0 to 9, and A to F (or a to f) symbols.
Example
请看以下示例:
Take a look at the following example −
#include <stdio.h>
int main(){
int oct = 025;
int hex = 0xa1;
printf("Octal to decimal: %d\n", oct);
printf("Hexadecimal to decimal: %d\n", hex);
}
运行此代码,您将获得以下输出−
On running this code, you will get the following output −
Octal to decimal: 21
Hexadecimal to decimal: 161
现代 C 编译器还允许您将整数表示为二进制数,为此您需要添加一个 0b 前缀。
Modern C compilers also let you represent an integer as a binary number, for which you need to add a 0b prefix.
Example
请看以下示例:
Take a look at the following example −
#include <stdio.h>
int main(){
int x = 0b00010000;
printf("binary to decimal: %d", x);
}
运行代码并检查其输出:
Run the code and check its output −
binary to decimal: 16
以下是整数文本的一些示例:
Here are some examples of integer literals −
212 /* valid */
215u /* valid */
0xFeeL /* valid */
078 /* invalid: 8 is not an octal digit */
032UU /* invalid: cannot repeat a suffix */
以下是其他一些各种类型的整数字面量示例−
Here are some 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 in C
C 中的浮点字面量是一个实数,它有一个整数部分和小数部分,其范围为所用编译器接受的范围之内,并用数字表示,小数点带有一个可选的指数符号(e 或 E)。
A floating-point literal in C is a real number with an integer part and a fractional part within the range acceptable to the compiler in use, and represented in digits, decimal point with an optional exponent symbol (e or E).
浮点字面量通常用于初始化或设置 C 中浮点数或双精度变量的值。
A floating point literal is generally used for initializing or setting the value of a float or a double variable in C.
Example
以下赋值示例使用浮点字面量,小数点分隔整数和小数部分−
The following assignment examples use floating point literals with a decimal point separating the integer and the fractional part −
#include <stdio.h>
int main(){
float x = 10.55;
float y = -1.333;
printf("x and y are: %f, %f", x, y);
}
你将获得以下输出 -
You will get the following output −
x and y are: 10.550000, -1.333000
具有高度精度的浮点字面量可以用指数符号“e”或“E”表示。这称为 scientific notation of a float literal 。
Floating point literals with a high degree of precision can be stated with the exponentiation symbol "e" or "E". This is called the scientific notation of a float literal.
在表示十进制形式时,您必须包括小数点、指数或两者。在表示指数形式时,您必须包括整数部分、小数部分或两者。
While representing decimal form, you must include the decimal point, the exponent, or both. While representing exponential form, you must include the integer part, the fractional part, or both.
Example
请看以下示例:
Take a look at the following example −
#include <stdio.h>
int main(){
float x = 100E+4;
float y = -1.3E-03;
printf("x: %f\n", x);
printf("y: %f\n", y);
}
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
x: 1000000.000000
y: -0.001300
以下是浮点数文本的一些示例:
Here are some examples of floating-point literals −
3.14159 /* valid */
314159E-5L /* valid */
510E /* invalid: incomplete exponent */
210f /* invalid: no decimal or exponent */
.e55 /* invalid: missing integer or fraction */
Character Literals in C
C 中的字符字面量是单引号符号中包含的单个字符。请注意,C 仅识别直引号。因此,使用 ' 形成字符字面量,而不是 ‘ )。以下是示例−
A character literal in C is a single character enclosed within single quote symbols. Note that C recognizes straight quotes only. Hence, use ' to form a character literal and not ‘). Here is an example −
char x = 'I';
字符字面量通常分配给一个占一个字节的 char 变量。使用 %c 格式说明符输出字符。使用 %d ,您将获得字符的 ASCII 值。
Character literals are generally assigned to a char variable that occupies a single byte. Using the %c format specifier outputs the character. Use %d and you’ll obtain the ASCII value of the character.
Escape Sequences in C
C 将许多转义序列定义为以“\”开头的字符序列,并且附加到后续字符的备用含义。
C defines a number of escape sequences as a sequence of characters starting with "\" and an alternate meaning attached to the following characters.
即使转义序列包括多个字符,它也会放在单引号中。转义序列产生单个不可打印字符的效果。例如, '\n' 是代表换行符的转义序列,其效果与按下 Enter 键相同。
Even though an escape sequence consists of more than one characters, it is put inside single quotes. An escape sequence produces the effect of a single non-printable character. For example, '\n' is an escape sequence that represents a newline character, with the same effect as pressing the Enter key.
Example
请看以下示例:
Take a look at the following example −
#include <stdio.h>
int main(){
char x = 'I';
char y = 'J';
printf("x: %c\ny: %c", x,y);
}
在这里,您将获得此输出−
Here you will get this output −
x: I
y: J
我们将在后面的章节中详细了解转义序列。
We shall learn more about escape sequences in a later chapter.
字符字面量也可以是字符的UNICODE表示。此类字面量在开头有 /u 。
A character literal can also be a UNICODE representation of a character. Such a literal has /u at the beginning.
String Literals in C
放在双引号符号中的字符序列形成字符串字面量。C 不提供字符串变量。相反,我们需要使用 char 类型数组来存储字符串。
A sequence of characters put inside double quotation symbols forms a string literal. C doesn’t provide a string variable. Instead, we need to use an array of char type to store a string.
Example
请看以下示例:
Take a look at the following example −
#include <stdio.h>
int main(){
char arr[] = "Hello World";
printf("arr: %s", arr);
}
运行代码并检查其输出:
Run the code and check its output −
arr: Hello World
字符串字面量可以包含普通字符、转义序列和 Unicode 字符。例如−
A string literal may contain plain characters, escape sequences, and Unicode characters. For example −
char arr[] = "Hello \
World";
您还可以通过将元素放在花括号 { 和 } 中,来获得数组的字面量表示。例如:
You can also have a literal representation of an array by putting its elements inside the curly brackets { and }. For example:
int arr[] = {10, 20, 30, 40};
类似地,花括号也可以用于结构值的字面量表示。例如−
Similarly, the curly brackets can also be used for a literal representation of a struct value. For example −
struct marks {
int phy;
int che;
int math
};
struct marks m1 = {50, 60, 70};
后面章节中我们将详细了解数组和结构。
We shall learn about arrays and structures in detail later in this tutorial.