简体   繁体   English

最大标识符长度

[英]Max identifier length

Where can I find what is the maximum identifier length in C? 在哪里可以找到C中的最大标识符长度?

In which header file is that limit specified? 哪个头文件是指定的限制?

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) 内部标识符或宏名称中的63个重要初始字符(每个通用字符名称或扩展源字符被视为单个字符)
  • 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) 外部标识符中的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. 因此,您应该检查文档,看看您的编译器是否支持标识符中更多的重要字符。

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. 曾几何时,对于外部变量,您只能依赖6个唯一字符(单例) - 因为这是一些大型机环境提供的。 (This is what the C89 standard said - but it noted that the limitation was painful.) (这是C89标准所说的 - 但它注意到这种限制是痛苦的。)

These days, in part because of type-safe linkage in C++, you can reasonably rely on much longer names for external symbols. 这些天,部分原因是由于C ++中的类型安全链接,您可以合理地依赖外部符号的更长名称。 If you start drifting above 31 characters, you may run into problems - but you are also running into readability problems too. 如果你开始漂移超过31个字符,你可能会遇到问题 - 但你也遇到了可读性问题。

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. 简而言之,没有头文件可以告诉您,这是ANSI / ISO C标准规范的一部分,它定义了C语言本身的语法和环境机制的布局。 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. 在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. 今天,最新的标准是C99标准,它规定标识符的最大长度为32,原因很简单和逻辑......编译器通过解析输入流来作为命令行参数传递, makefile或解决方案(对于Microsoft Visual Studio环境),解析器是严格的并且是固定的,因此对标识符的长度施加了限制,以便解析器可以向前看并查看是否还有其他字符。 It's down to that reason for it. 这是因为它的原因。

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指出的那样,可能会混淆编译器以及代码的链接。

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将命中截断的标识符,如果编译器没有截断,则命中完整标识符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM