c++ - `char _3 = ' c'` 在 C 和 C++ 中有效吗?

标签 c++ c variables identifier

考虑到,我已将变量名称声明为带下划线的数字。

#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/

相关文章:

c++ - 如何判断一个类是否间接继承自基类?

c++ - 如何使用 Doxygen 记录 SFINAE 启用的功能?

c++ - 将已解析序列的每个元素传递给返回规则属性类型的函数

python - 变量的显示值

c++ - 根据枚举成员的值特化模板

C:将许多字符插入 malloc 生成的字符串中

从 gcc 的中间文件编译目标文件

c - K&R 波兰语计算器中可能存在的错误

MySQL过程语法错误,如何为连接变量赋值?

javascript - if (var.length >0){} 和 if (var){} 之间的区别