C++primer中标识符问题

evilotus 2009-03-09 12:59:39
c++ primer第四版第二章“术语”中对标识符的定义:

identifier(标识符)
A name. Each identifier is a nonempty sequence of letters, digits, and underscores that must not begin with a digit. Identifiers are case-sensitive: Upper- and lowercase letters are distinct. Identifiers may not use C++ keywords. Identifiers may not contain two adjacent underscores nor may they begin with an underscore followed by a uppercase letter.

名字。每个标识符都是字母、数字和下划线的非空序列,且序列不能以数字开头。标识符是大小写敏感的:大写字母和小写字母含义不同。标识符不能使用C++中的关键字,不能包含相邻的下划线,也不能以下划线后跟一个大写字母开始。

///////////////////////////////////////

关于“不能包含相邻的下划线,也不能以下划线后跟一个大写字母开始。”这一句我有疑问啊,好像可以这样命名的吧。下面的程序我在DEVC++和VC6里都成功运行了。请高人指点。

#include<iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
int a = 1;
int _a = 2;
int __a = 3;
int _ABC = 4;
int __a__ = 5;
int _____ = 6;

cout << a << endl;
cout << _a << endl;
cout << __a << endl;
cout << _ABC << endl;
cout << __a__ << endl;
cout << _____ << endl;

system("pause");
return 0;
}



...全文
96 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
evilotus 2009-03-09
  • 打赏
  • 举报
回复
OK. 明白了,“不能包含相邻的下划线,也不能以下划线后跟一个大写字母开始”可能是编译器定义标识符的方式,所以我们最好别用。谢谢大家了。
pengzhixi 2009-03-09
  • 打赏
  • 举报
回复
不能包含相邻的下划线,也不能以下划线后跟一个大写字母开始
这么说是因为有点编译器在处理名称的时候会进行变名的处理,很多时候处理后的名称都是下划线加字母开始的。其实这个并没什么的。
  • 打赏
  • 举报
回复
我测了也可以啊,通过就ok啊,楼主太较真了吧.
brookmill 2009-03-09
  • 打赏
  • 举报
回复
连续下划线应该是能用的吧,系统代码里用的很多

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧