Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 375
Moshe Avatar asked Sep 12 '11 19:09

Moshe


People also ask

Is there any limitation for identifier length?

The following table describes the maximum length for each type of identifier. Aliases for column names in CREATE VIEW statements are checked against the maximum column length of 64 characters (not the maximum alias length of 256 characters).

What is the maximum length of C variable name?

ANSI standard recognizes a length of 31 characters for a variable name. However, the length should not be normally more than any combination of eight alphabets, digits, and underscores. 4. Uppercase and lowercase are significant.

What is the maximum number of an identifier?

65535 bytes in an object (in a hosted environment only)

What is the size of identifier?

The length of the identifiers should not be more than 31 characters. Identifiers should be written in such a way that it is meaningful, short, and easy to read.


2 Answers

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

like image 87
Ben Voigt Avatar answered Sep 21 '22 20:09

Ben Voigt


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

like image 26
Felix Dombek Avatar answered Sep 25 '22 20:09

Felix Dombek