为什么 $ 符号不能用作标识符的一部分?
Why the $ symbol can not be used as a part of an identifier?
我是c++的新手。我在一本 C++ 书中发现了以下语句:"In any C++ program, a variable name starts with a letter and contains only letters, digits, and underscores.The following are not variable name:"
2x // a name must start with a letter
time$to$market // $ is not a letter, digit, or underscore
Start menu // space is not a letter, digit, or underscore
问题是为什么time$to$market
不是变量名?
我尝试编译它并且编译器没有抱怨,我使用的编译器是 MinGW 32bit for c++ in QT。但是它应该抱怨!
I tried to use it and the compiler did not give error.
只是不要这样做,即使您当前的编译器支持它(正如 Quentin 在评论中指出的那样)。它不在标准中,因此您的代码将无法移植到其他环境,甚至无法移植到使用不同标准的同一编译器(尝试 GCC 选项 -ansi
、-std=c++11
etc.)。可能发生的最好情况是您在编译时遇到错误,最糟糕的情况是您将花费数小时寻找未定义的行为。
编译器有不同的变体。一种可能编译得更快,另一种可能由于某些原因与某些默认设置不同。不知道你用的是什么编译器。但是如果你对你的编译器进行一些研究,你会发现 $ 是允许的。尝试使用不同的编译器进行编译以完全赶上您的书。查看此主题以了解纯 C++ 的含义。因为书通常教纯语言。
What is pure C++
这完全取决于您使用的编译器,因为我在 TC 4.5 中使用它显示此错误:
Compiling DEMO.C:
Illegal character '$' (0x24)
一些编译器可能支持它。
我是c++的新手。我在一本 C++ 书中发现了以下语句:"In any C++ program, a variable name starts with a letter and contains only letters, digits, and underscores.The following are not variable name:"
2x // a name must start with a letter
time$to$market // $ is not a letter, digit, or underscore
Start menu // space is not a letter, digit, or underscore
问题是为什么time$to$market
不是变量名?
我尝试编译它并且编译器没有抱怨,我使用的编译器是 MinGW 32bit for c++ in QT。但是它应该抱怨!
I tried to use it and the compiler did not give error.
只是不要这样做,即使您当前的编译器支持它(正如 Quentin 在评论中指出的那样)。它不在标准中,因此您的代码将无法移植到其他环境,甚至无法移植到使用不同标准的同一编译器(尝试 GCC 选项 -ansi
、-std=c++11
etc.)。可能发生的最好情况是您在编译时遇到错误,最糟糕的情况是您将花费数小时寻找未定义的行为。
编译器有不同的变体。一种可能编译得更快,另一种可能由于某些原因与某些默认设置不同。不知道你用的是什么编译器。但是如果你对你的编译器进行一些研究,你会发现 $ 是允许的。尝试使用不同的编译器进行编译以完全赶上您的书。查看此主题以了解纯 C++ 的含义。因为书通常教纯语言。
What is pure C++
这完全取决于您使用的编译器,因为我在 TC 4.5 中使用它显示此错误:
Compiling DEMO.C:
Illegal character '$' (0x24)
一些编译器可能支持它。