cpp cout中的单引号
Single Quote in cpp cout
摘自《编程:使用 C++ 的原则与实践》第 72–73 页:
We saw that we couldn’t directly add chars or compare a double to an int.
However, C++ provides an indirect way to do both. When needed, a char is
converted to an int and an int is converted to a double. For example:
char c = 'x';
int i1 = c;
int i2 = 'x';
Here both i1 and i2 get the value 120, which is the integer value of the
character 'x' in the most popular 8-bit character set, ASCII. This is a simple
and safe way of getting the numeric representation of a character. We call this
char-to-int conversion safe because no information is lost; that is, we can
copy the resulting int back into a char and get the original value:
char c2 = i1;
cout << c << ' << i1 << ' << c2 << '\n';
This will print x 120 x
我不明白这里使用的单引号。当我尝试时,它打印 x540818464x.
您应该试试这段代码(如下)。你或那本书打错字了。
#include <iostream>
using namespace std;
int main() {
char c = 'x';
int i1 = c;
int i2 = 'x';
char c2 = i1;
std::cout << c << ' ' << i1 << ' ' << c2 << '\n';
return 0;
}
结果:
对该代码的唯一解释是这本书印刷错误,或者这是一个明显的拼写错误。正确的代码应该是
cout << c << ' ' << i1 << ' ' << c2 << '\n';
' << i1 << '
是一个多字符文字,具有类型 int
和实现定义的值。
你可能想要:cout << c << ' ' << i1 << ' ' << c2 << '\n';
使用正则字符 space.
这看起来确实像一个错字,我们可以安全地推测作者的意思:
cout << c << ' ' << i1 << ' ' << c2 << '\n';
不正确的代码 虽然 可以编译,但是没有意义:
cout << c << ' << i1 << ' << c2 << '\n';
在这里,' << i1 << '
(注意单引号)被称为多字符文字,它具有类型int
和实现定义的值。
From [lex.ccon]/2
:
An ordinary character literal that contains more than one c-char is a
multicharacter literal. A multicharacter literal [...] is conditionally-supported, has type int, and
has an implementation-defined value.
它的用法相对较少,我个人认为它是一种定义任意常量的方法,例如
enum state { wait = 'wait', start = 'start', /*...*/ };
通过使用单引号,我们可以打印ascii值。所以打印了整个表达式的 ascii 值。
谢谢我希望这会有所帮助
摘自《编程:使用 C++ 的原则与实践》第 72–73 页:
We saw that we couldn’t directly add chars or compare a double to an int. However, C++ provides an indirect way to do both. When needed, a char is converted to an int and an int is converted to a double. For example:
char c = 'x';
int i1 = c;
int i2 = 'x';
Here both i1 and i2 get the value 120, which is the integer value of the character 'x' in the most popular 8-bit character set, ASCII. This is a simple and safe way of getting the numeric representation of a character. We call this char-to-int conversion safe because no information is lost; that is, we can copy the resulting int back into a char and get the original value:
char c2 = i1;
cout << c << ' << i1 << ' << c2 << '\n';
This will print x 120 x
我不明白这里使用的单引号。当我尝试时,它打印 x540818464x.
您应该试试这段代码(如下)。你或那本书打错字了。
#include <iostream>
using namespace std;
int main() {
char c = 'x';
int i1 = c;
int i2 = 'x';
char c2 = i1;
std::cout << c << ' ' << i1 << ' ' << c2 << '\n';
return 0;
}
结果:
对该代码的唯一解释是这本书印刷错误,或者这是一个明显的拼写错误。正确的代码应该是
cout << c << ' ' << i1 << ' ' << c2 << '\n';
' << i1 << '
是一个多字符文字,具有类型 int
和实现定义的值。
你可能想要:cout << c << ' ' << i1 << ' ' << c2 << '\n';
使用正则字符 space.
这看起来确实像一个错字,我们可以安全地推测作者的意思:
cout << c << ' ' << i1 << ' ' << c2 << '\n';
不正确的代码 虽然 可以编译,但是没有意义:
cout << c << ' << i1 << ' << c2 << '\n';
在这里,' << i1 << '
(注意单引号)被称为多字符文字,它具有类型int
和实现定义的值。
From
[lex.ccon]/2
:An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter literal [...] is conditionally-supported, has type int, and has an implementation-defined value.
它的用法相对较少,我个人认为它是一种定义任意常量的方法,例如
enum state { wait = 'wait', start = 'start', /*...*/ };
通过使用单引号,我们可以打印ascii值。所以打印了整个表达式的 ascii 值。
谢谢我希望这会有所帮助