What is a “literal” in C++? [duplicate]

最后都变了- 提交于 2019-12-03 05:11:50

问题


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++.


回答1:


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++.

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




回答2:


Wikipedia gives you quickly this about literals.

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



来源:https://stackoverflow.com/questions/14111329/what-is-a-literal-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!