Search code examples
c++gcclanguage-design

Is there a limit to the length of identifier names in C++?


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?


Solution

  • 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