Where can I find what is the maximum identifier length in C?开发者_运维百科
In which header file is that limit specified?
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.
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
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.
精彩评论