考虑到,我已将变量名称声明为带下划线的数字。
#include <stdio.h>
int main()
{
char _3 = 'c';
printf("%c\n",_3);
}
我想知道,它在 C 和 C++ 中工作得很好。那么,它有效吗?
最佳答案
所有变量名称必须以字母或 下划线。所以是的,它是有效的,除非你将它放在文件范围内。 (不过要小心双下划线,它是为编译器内部使用保留的)
但是,我不建议使用这样的名称的变量,因为它可能会让读者感到困惑。
来自 C++ 2003 标准:
17.4.3.1.2 Global names [lib.global.names]
Certain sets of names and function signatures are always reserved to the implementation:
- Each name that contains a double underscore (_ _) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use.
- Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.165
165) Such names are also reserved in namespace ::std (17.4.3.1).
关于c++ - `char _3 = ' c'` 在 C 和 C++ 中有效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47120549/