Max identifier length

There is no header that tells you. You have to make an informed decision based on the platforms to which you are likely to be porting. Carl Norum pointed out what the C99 standard says.

Once upon a time, you could only rely on 6 unique characters, mono-case, for external variables - because that was what some mainframe environments provided. (This is what the C89 standard said - but it noted that the limitation was painful.)

These days, in part because of type-safe linkage in C++, you can reasonably rely on much longer names for external symbols. If you start drifting above 31 characters, you may run into problems - but you are also running into readability problems too.


Since there are some bizarre corner cases where it is helpful to have code aware of the limit, here is a method that can be placed in a (albeit hideous to look at) header file:

#define SOMEREALLYREALLY...REALLYLONGNAME 1
#if SOMEREALLYREALLY
#define MAXIDENT 16
#elif SOMEREALLYREALLYR
#define MAXIDENT 17
#elif SOMEREALLYREALLYRE
#define MAXIDENT 18
...and so on

Eventually, the #ifs will either hit truncated identifier, or the full identifier if the compiler doesn't truncate


There is no header file to contain the identifier length limit; even if there were, how could it help you? You can't change your identifier lengths at compile time based on a value in a header file anyway.

The C standard, section 5.2.4.1 says:

  • 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)

It also contains a footnote:

Implementations should avoid imposing fixed translation limits whenever possible.

So you should check your documentation to see if your compiler supports a greater number of significant characters in identifiers.

Tags:

C