Devel
Devel

Reputation: 960

Max identifier length

Where can I find what is the maximum identifier length in C?

In which header file is that limit specified?

Upvotes: 20

Views: 30937

Answers (5)

mtraceur
mtraceur

Reputation: 3726

It sounds like what you really want is a macro which you can check with #if or #ifdef to decide how long of identifiers to use.

The macro you want is __STDC_VERSION__, which was first defined in C99 (if you want to be more sure that an older compiler isn't defining that macro for some reason, then instead of #ifdef __STDC_VERSION__ you could do #if __STDC_VERSION__ >= 199901L).

C99 is also the same standard that raised the minimum lengths of identifiers that C compilers were supposed to support. For example, 32 instead of 6 characters for the minimum that an external identifier was supposed to handle.

But I encourage you to remember that first of all, names are an API and sometimes even ABI surface! So maybe don't change names based on properties of the compiler! If I write code that uses your library, and you made your library change names like that, I have to immitate your test for label size. Now my code has to have a bunch of preprocessor directives selecting different names!

Normally, you should change it based on a macro that your users (other developers and people building from source) are able to define if they need compatibility with an ancient compiler or system with such limits.

So it's often better to pick a clear macro name like MYLIBRARY_SHORT_NAMES, and #ifdef or #if for that. Document it in your documentation, perhaps in a "portability" section, for the vanishingly small number of users who will need short names. Document the minimum significant characters needed for the normal names, and for the short names, and let your users figure it out from there in the rare case that they need to.

Upvotes: 0

Sean K
Sean K

Reputation: 794

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

Upvotes: 6

t0mm13b
t0mm13b

Reputation: 34592

In short, no header file exists to tell you that, that is part of a ANSI/ISO C Standard specifications which defines the layout of the syntax and environment mechanism for the C language itself. In pre C89 standards, the maximum identifier length was 6, due to the small memory footprints and environment on such systems such as mainframes and *nix systems.

Today, the latest standard is C99 standards which dictate that the maximum length for an identifier is to be 32, the reason is quite simple and logical...the compiler works by parsing the input stream which would be passed as a command line argument, makefile, or a solution (for Microsoft Visual Studio environments), the parser is rigid and fixed and hence the imposed restrictions on the length of the identifier so that the parser can look ahead and see if there's any more characters. It's down to that reason for it.

Another reason is that most C++ compilers use name mangling on the identifiers which, as Jonathan Leffler pointed out, could confuse the compiler and also the linkage of the code.

Upvotes: 3

Carl Norum
Carl Norum

Reputation: 224844

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.

Upvotes: 29

Jonathan Leffler
Jonathan Leffler

Reputation: 753455

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.

Upvotes: 11

Related Questions