变量名中的美元符号
我偶然发现了一些这样的 C++ 代码:
int $T$S;
首先我认为这是某种 PHP 代码或者错误地粘贴在那里,但它编译并运行得很好(在 MSVC 2008 年)。
什么样的字符对 C++ 中的变量有效?还有其他奇怪的字符可以使用吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据标准,唯一合法的字符是字母数字
和下划线。该标准确实要求任何事情
Unicode 认为字母是可以接受的(但仅作为单个
码位字符)。在实践中,实现提供了扩展
(即有些确实接受 $)和限制(大多数不接受所有
所需的 Unicode 字符)。如果你希望你的代码是可移植的,
将符号限制为 26 个不带重音的字母(大写或小写)
十位数字和“_”。
The only legal characters according to the standard are alphanumerics
and the underscore. The standard does require that just about anything
Unicode considers alphabetic is acceptable (but only as single
code-point characters). In practice, implementations offer extensions
(i.e. some do accept a $) and restrictions (most don't accept all of the
required Unicode characters). If you want your code to be portable,
restrict symbols to the 26 unaccented letters, upper or lower case, the
ten digits, and the '_'.
它是某些编译器的扩展,而不是 C 标准
MSVC 中的扩展:
https://web.archive.org/web/20100216114436/http://msdn.microsoft.com/en-us/library/565w213d.aspx
最新版本:https://learn.microsoft.com /en-us/cpp/cpp/identifiers-cpp?redirectedfrom=MSDN&view=vs-2019
GCC:
http://gcc.gnu.org/onlinedocs/gcc/Dollar -Signs.html#Dollar-Signs
It's an extension of some compilers and not in the C standard
MSVC:
https://web.archive.org/web/20100216114436/http://msdn.microsoft.com/en-us/library/565w213d.aspx
Newest version: https://learn.microsoft.com/en-us/cpp/cpp/identifiers-cpp?redirectedfrom=MSDN&view=vs-2019
GCC:
http://gcc.gnu.org/onlinedocs/gcc/Dollar-Signs.html#Dollar-Signs
据我所知,只有字母(大写和小写)、数字(
0
到9
)和根据标准,_
对变量名称有效(注意:变量名称不应以数字开头)。所有其他字符都应该是编译器扩展。
To my knowledge, only letters (capital and small), numbers (
0
to9
) and_
are valid for variable names according to the standard (note: the variable name should not start with a number though).All other characters should be compiler extensions.
这不是一个好的做法。通常,您应该仅在标识符中使用字母数字字符和下划线 (
[az][AZ][0-9]_
)。表面层
与其他语言(bash、perl)不同,C 不使用
$
来表示变量的使用。因此,它在技术上是有效的。在 C 语言中,它很可能属于 C11,6.4.2。这意味着现代编译器似乎确实支持它。至于你的C++问题,让我们测试一下!
在 GCC/G++/Clang/Clang++ 上,这确实可以编译并运行得很好。
更深层次的
编译器获取源代码,将其词法到令牌流中,将其放入抽象语法树(AST)中,然后使用它来生成代码(例如汇编/LLVM IR)。你的问题实际上只围绕第一部分(例如词法分析)。
C/C++ 的语法(因此是词法分析器实现)不会将
$
视为特殊的,与逗号、句点、细箭头等不同......因此,您可能会从词法分析器获得如下输出:来自下面的 C 代码:在词法分析器之后,这变成了一个令牌流,如下所示:
如果您在哪里获取此代码:
词法分析器将输出一个令牌流,如下所示:
如您所见,因为 C/C++ 不处理字符就像 $ 一样特别,它处理方式与其他字符(如句点)不同。
This is not good practice. Generally, you should only use alphanumeric characters and underscores in identifiers (
[a-z][A-Z][0-9]_
).Surface Level
Unlike in other languages (bash, perl), C does not use
$
to denote the usage of a variable. As such, it is technically valid. In C it most likely falls under C11, 6.4.2. This means that it does seem to be supported by modern compilers.As for your C++ question, lets test it!
On GCC/G++/Clang/Clang++, this indeed compiles, and runs just fine.
Deeper Level
Compilers take source code, lex it into a token stream, put that into an abstract syntax tree (AST), and then use that to generate code (e.g. assembly/LLVM IR). Your question really only revolves around the first part (e.g. lexing).
The grammar (thus the lexer implementation) of C/C++ does not treat
$
as special, unlike commas, periods, skinny arrows, etc... As such, you may get an output from the lexer like this from the below c code:After the lexer, this becomes a token steam like such:
If you where to take this code:
The lexer would output a token steam like:
As you can see, because C/C++ doesn't treat characters like $ as special, it is processed differently than other characters like periods.