开发者

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

I don't remember the standard saying somethin开发者_C百科g 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?


  • Microsoft C++: 2048 characters
  • Intel C++: 2048 characters
  • g++: No limit, as answered in this question: is there a length limit on g++ variable names?


Annex B of the C++ Standard says that an implementation should support identifiers at least 1024 characters long, but this is not mandatory.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜