什么是 C++ 中的“文字”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14111329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:59:22  来源:igfitidea点击:

What is a "literal" in C++?

c++terminology

提问by Amani

Possible Duplicate:
What does the word “literal” mean?

可能的重复:
“文字”这个词是什么意思?

Often when reading literature about C++, I encounter the word "literal". It is a bit unclear to me what exactly this term means in C++.

通常在阅读有关 C++ 的文献时,我会遇到“文字”这个词。我有点不清楚这个术语在 C++ 中的确切含义。

回答by Lightness Races in Orbit

A literal is some data that's presented directly in the code, rather than indirectly through a variable or function call.

文字是一些直接在代码中呈现的数据,而不是通过变量或函数调用间接呈现的。

Here are some examples, one per line:

以下是一些示例,每行一个:

42
128
3.1415
'a'
"hello world"

The data constituting a literal cannot be modified by a program, but it may be copied into a variable for further use:

构成文字的数据不能被程序修改,但可以将其复制到变量中以供进一步使用:

int a = 42;  // creates variable `a` with the same value as the literal `42`

This concept is by no means unique to C++.

这个概念绝不是 C++ 独有的

The term "literal" comes from the fact that you've written data literallyinto your program, i.e. exactly as written, not "hidden" behind a variable name.

术语“文字”来自这样一个事实,即您已将数据按字面意思写入程序,即完全按照所写的方式写入,而不是“隐藏”在变量名后面。

回答by Basile Starynkevitch

Wikipedia gives you quickly this about literals.

Wikipedia 为您提供了有关文字的快速信息。

In your C or C++ source code, Things like 1234, nullptr(in recent C++), "abcd"are literals.

在您的 C 或 C++ 源代码中,诸如1234, nullptr(在最近的 C++ 中)之类的东西"abcd"是文字。