tags:

views:

66

answers:

3

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

In which header file is that limit specified?

+5  A: 

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.4.2.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.

Carl Norum
In which header file is that limit specified?
Devel
It is not in a header file, it is in your compiler's documentation. Identifiers disappear at runtime.
Hans Passant
@Devel, there is no such thing. Even if there were, how would it help you? You can't change your identifier lengths based on a header file, can you?
Carl Norum
@Carl, you could imagine some macro preprocessing building identifiers in one way or the other depending on the length limit. It also could be used in code generation.
AProgrammer
@AProgrammer, those are good suggestions.
Carl Norum
+3  A: 

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.

Jonathan Leffler
+1 for C89 having only 6 characters for external identifiers.
Carl Norum
+1  A: 

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 which could confuse the compiler and also, the linkage of the code.

Hope this helps, Best regards, Tom.

tommieb75