简体   繁体   中英

What is max length for an C/C++ identifier on common (build) systems?

I don't remember the standard saying something of the max length for identifiers so in theory they can be long . In real life, those names could be limited by at least the compiler and linker implementations.

While this should work on all systems

int a;

this snippet

#!/usr/bin/perl
print "int" . "b" x 2**16 . ";";

creates a declarationen that gives undefined reference to std:: somethings with ld while compiling/linking (using gcc/mingw).

So what are the size limits for an identifier on different systems?

C++ 标准的附录 B 说一个实现应该支持长度至少为 1024 个字符的标识符,但这不是强制性的。

Based on MISRA C 2004:

Rule 5.1 (required): Identifiers (internal and external) shall not rely on the significance of more than 31 characters. [Undefined 7; Implementation 5, 6] The ISO standard requires internal identifiers to be distinct in the first 31 characters to guarantee code portability. This limitation shall not be exceeded, even if the compiler supports it. This rule shall apply across all name spaces. Macro names are also included and the 31 character limit applies before and after substitution. The ISO standard requires external identifiers to be distinct in the first 6 characters, regardless of case, to guarantee optimal portability. However this limitation is particularly severe and is considered unnecessary. The intent of this rule is to sanction a relaxation of the ISO requirement to a degree commensurate with modern environments and it shall be confirmed that 31 character/ case significance is supported by the implementation. Note that there is a related issue with using identifier names that differ by only one or a few characters, especially if the identifier names are long. The problem is heightened if the differences are in easily mis-read characters like 1 (one) and l (lower case L), 0 and O, 2 and Z, 5 and S, or n and h. It is recommended to ensure that identifier names are always easily visually distinguishable. Specific guidelines on this issue could be placed in the style guidelines (see section 4.2.2).

I use this rule. Maybe somebody asks me "Do you see any compiler that can't recognize identifier more than 31?" Yes, I remember in IAR RL78 v2.21.1 I defined 2 identifier without any warning and error, but I faced problem in access(now I don't remember what was the exact scenario)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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