ccompiler-constructionlanguage-lawyerc99

Translation limits of an identifier in C


I have started to write my own C compiler for the purpose of education. My goal is to be mostly C99 standard conform. Now I have a question about the maximum length of an identifier.

This is an extract from the C99 specification about the identifiers length:

  • 63 significant initial characters in an internal identifier or a macro name (each universal character name or extended source character is considered a single character)
  • 31 significant initial characters in an external identifier (each universal character name specifying a short identifier of 0000FFFF or less is considered 6 characters, each universal character name specifying a short identifier of 00010000 or more is considered 10 characters, and each extended source character is considered the same number of characters as the corresponding universal character name, if any)

C99 (N1256) Section 5.2.4.1

As I understand correct, the maximal length is 63 characters for local identifier (local variable names or static function names) and 31 characters for externally identifiers. If I'm totally wrong please correct me.

On another page from the standard I have read following:

An identifier is a sequence of nondigit characters (including the underscore _, the lowercase and uppercase Latin letters, and other characters) and digits, which designates one or more entities as described in 6.2.1. Lowercase and uppercase letters are distinct. There is no specific limit on the maximum length of an identifier.

C99 (N1256) Section 6.4.2.1

I have marked my misunderstood sentence.

Please can somebody clarify the real limits of an identifier? Because in one text passage there is a limit, in the other text passage not really.

There exists two similar questions on SO:
limit on the length of an identifier in C
Maximum length of identifiers in Turbo C++ and Dev-C++ for both C and C++ languages

Currently I don't know which limits are useful. I also don't tried with GCC (as example) if there is a soft limit of 31 or 63 characters if the compiler flag -std=c99 is set.


Solution

  • Please can somebody clarify the real limits of an identifier? Because in one text passage there is a limit, in the other text passage not really.

    You are not reading the first source correct.

    It starts with :

    The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits:

    And after the list there is a footnote:

    1. Implementations should avoid imposing fixed translation limits whenever possible.

    That means, this section does not define any upper limit. It just demands the the compiler must support at least these numbers and there should not be any limit if possible. That makes the list a list of lower limits. If you have sufficient memory to compile a program, then you should not fail to do so just because some identifier is rather long.

    As a conclusion, this part

    Currently I don't know which limits are useful.

    can be answered by:

    Your RAM is the limit. But make sure that you can handle at least the numbers given in the standard.