13

Is there a length limit to the names of variables in C++? What is it? Does this have anything to do with the "64/32-bitness" of the machine?

EDIT: Specifically, what is GCC's limit?

Moshe
  • 57,511
  • 78
  • 272
  • 425

2 Answers2

24

section lex.name of the C++ standard says

An identifier is an arbitrarily long sequence of letters and digits.

However, variable names which share a very large number of initial characters may not be treated as separate variables, the exact number of initial characters used is implementation-specific. Annex B says:

Because computers are finite, C++ implementations are inevitably limited in the size of the programs they can successfully process. Every implementation shall document those limitations where known. This documentation may cite fixed limits where they exist, say how to compute variable limits as a function of available resources, or say that fixed limits do not exist or are unknown.

The limits may constrain quantities that include those described below or others. The bracketed number following each quantity is recommended as the minimum for that quantity. However, these quantities are only guidelines and do not determine compliance.


For gcc, the limits are:

Preprocessor: no limit

C language: no limit

C++: Probably same as C, no separate limit documented. "Some choices are documented in the corresponding document for the C language"

Linker (controls external names linked across compilation units): Platform-specific, often unlimited

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 1
    What does GCC's implementation set the limit at? – Moshe Sep 12 '11 at 19:23
  • @Moshe: I can only find documentation for the gcc preprocessor, which says [there is no limit](http://gcc.gnu.org/onlinedocs/cpp/Implementation-limits.html) and the gcc C compiler, which [also has no limit](http://gcc.gnu.org/onlinedocs/gcc/Identifiers-implementation.html#Identifiers-implementation). I suspect the gcc C++ compiler has the same limits as C. – Ben Voigt Sep 12 '11 at 19:58
  • @Moshe: from the page I linked: "For internal names, all characters are significant. For external names, the number of significant characters are defined by the linker; for almost all targets, all characters are significant." – Ben Voigt Sep 12 '11 at 20:10
7

In MS Visual Studio 2003–2012 the maximum length of an identifier is 2047 characters (per MSDN).

Felix Dombek
  • 13,664
  • 17
  • 79
  • 131