C语言 最大标识符长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2352209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Max identifier length
提问by Devel
Where can I find what is the maximum identifier length in C?
我在哪里可以找到 C 中的最大标识符长度是多少?
In which header file is that limit specified?
该限制在哪个头文件中指定?
回答by Carl Norum
There is no header file to contain the identifier length limit; even if there were, how could it help you? You can't change your identifier lengths at compile time based on a value in a header file anyway.
没有包含标识符长度限制的头文件;即使有,它又如何帮助你?无论如何,您无法在编译时根据头文件中的值更改标识符长度。
The C standard, section 5.2.4.1 says:
在C标准,部分5.2.4.1说:
- 63 significant initial characters in an internal identifier or a macro name (each universal character name or extended source character is considered a single character)
- 31 significant initial characters in an external identifier (each universal character name specifying a short identifier of 0000FFFF or less is considered 6 characters, each universal character name specifying a short identifier of 00010000 or more is considered 10 characters, and each extended source character is considered the same number of characters as the corresponding universal character name, if any)
- 内部标识符或宏名称中的 63 个重要初始字符(每个通用字符名称或扩展源字符被视为单个字符)
- 外部标识符中的 31 个有效起始字符(每个指定短标识符为 0000FFFF 或更少的通用字符名称被视为 6 个字符,每个指定短标识符为 00010000 或更多的通用字符名称被视为 10 个字符,每个扩展源字符被视为与相应的通用字符名称相同的字符数(如果有)
It also contains a footnote:
它还包含一个脚注:
Implementations should avoid imposing fixed translation limits whenever possible.
实现应尽可能避免强加固定的翻译限制。
So you should check your documentation to see if your compiler supports a greater number of significant characters in identifiers.
所以你应该检查你的文档,看看你的编译器是否支持标识符中更多的有效字符。
回答by Jonathan Leffler
There is no header that tells you. You have to make an informed decision based on the platforms to which you are likely to be porting. Carl Norum pointed out what the C99 standard says.
没有标题可以告诉您。您必须根据可能要移植到的平台做出明智的决定。Carl Norum 指出了 C99 标准所说的内容。
Once upon a time, you could only rely on 6 unique characters, mono-case, for external variables - because that was what some mainframe environments provided. (This is what the C89 standard said - but it noted that the limitation was painful.)
曾几何时,对于外部变量,您只能依赖 6 个独特的字符(单一大小写)——因为这是某些大型机环境所提供的。(这是 C89 标准所说的 - 但它指出限制是痛苦的。)
These days, in part because of type-safe linkage in C++, you can reasonably rely on much longer names for external symbols. If you start drifting above 31 characters, you may run into problems - but you are also running into readability problems too.
如今,部分由于 C++ 中的类型安全链接,您可以合理地依赖更长的外部符号名称。如果您开始超过 31 个字符,您可能会遇到问题 - 但您也会遇到可读性问题。
回答by t0mm13b
In short, no header file exists to tell you that, that is part of a ANSI/ISO C Standard specifications which defines the layout of the syntax and environment mechanism for the C language itself. In pre C89 standards, the maximum identifier length was 6, due to the small memory footprints and environment on such systems such as mainframes and *nix systems.
简而言之,不存在头文件来告诉您,这是 ANSI/ISO C 标准规范的一部分,该规范定义了 C 语言本身的语法和环境机制的布局。在 C89 之前的标准中,最大标识符长度为 6,这是由于诸如大型机和 *nix 系统等系统上的内存占用和环境较小。
Today, the latest standard is C99 standards which dictate that the maximum length for an identifier is to be 32, the reason is quite simple and logical...the compiler works by parsing the input stream which would be passed as a command line argument, makefile, or a solution (for Microsoft Visual Studio environments), the parser is rigid and fixed and hence the imposed restrictions on the length of the identifier so that the parser can look ahead and see if there's any more characters. It's down to that reason for it.
今天,最新的标准是 C99 标准,它规定标识符的最大长度为 32,原因非常简单和合乎逻辑……编译器通过解析将作为命令行参数传递的输入流来工作, makefile 或解决方案(对于 Microsoft Visual Studio 环境),解析器是严格且固定的,因此对标识符的长度施加了限制,以便解析器可以提前查看是否还有更多字符。归根结底就是这个原因。
Another reason is that most C++ compilers use name mangling on the identifiers which, as Jonathan Leffler pointed out, could confuse the compiler and also the linkage of the code.
另一个原因是大多数 C++ 编译器在标识符上使用名称修饰,正如 Jonathan Leffler 指出的那样,这可能会混淆编译器以及代码的链接。
回答by Sean K
Since there are some bizarre corner cases where it is helpful to have code aware of the limit, here is a method that can be placed in a (albeit hideous to look at) header file:
由于有一些奇怪的极端情况,让代码知道限制是有帮助的,这里有一个方法可以放在(虽然看起来很可怕)头文件中:
#define SOMEREALLYREALLY...REALLYLONGNAME 1
#if SOMEREALLYREALLY
#define MAXIDENT 16
#elif SOMEREALLYREALLYR
#define MAXIDENT 17
#elif SOMEREALLYREALLYRE
#define MAXIDENT 18
...and so on
Eventually, the #ifs will either hit truncated identifier, or the full identifier if the compiler doesn't truncate
最终,如果编译器不截断,#ifs 将命中截断的标识符或完整标识符

