Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the exact role of "significant characters" in C (variables)?

Tags:

c

What is the exact role of "significant characters" in C , especially in the field of variables? I have read the topic “(K&R) At Least the first 31 characters...”, but I really don't understand the exact rules of significant characters. The only thing I understand well is that this subject is extinct, but I still need to know!

like image 792
kaymas Avatar asked Aug 17 '13 15:08

kaymas


1 Answers

In the current C standard, ISO/IEC 9899:2011, section §5.2.4.1 Translation limits says:

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:18)

...
— 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)19)
...

18) Implementations should avoid imposing fixed translation limits whenever possible.
19) See ‘‘future language directions’’ (6.11.3).

§6.11.3 External names
¶1 Restriction of the significance of an external name to fewer than 255 characters (considering each universal character name or extended source character as a single character) is an obsolescent feature that is a concession to existing implementations.

This means that when dealing with names, internal names that are distinct within the first 63 characters must be treated as distinct by the compiler, but if you are misguided enough to create two (or more) identifiers that differ in the 64th character only (the first 63 are identical, but the 64th character in one is, say, 1 and in the other is z), then the compiler may legitimately, and without warning, treat those two identifiers as the same.

The limit on external names — names which affect the linker rather than the compiler proper — may be limited to as few as 31 characters. Consider:

extern int abcdefghijkjlmnopqrstuvwxyz123456;
extern int abcdefghijkjlmnopqrstuvwxyz123457;

These two declarations may be treated as referring to the same variable if the system (linker) limits you to 31 characters.

As the future directions section states, any limit shorter than 255 is 'obsolescent', meaning that you should not be limited by this before names are 255 characters long. But the standard does not mandate 255 characters as the limit yet.

History

Previous editions of the standard had smaller lower bounds on the upper limits of the lengths of names. The C89 standard only mandated 6 characters monocase for external names (but it was regarded as a painful concession to existing linkers), so strcmp and StrCmp could be the same, as could abcdefg and abcdefz. Part of the trouble may have been Fortran; it only required support for 6 character monocase names, so linkers on systems where Fortran was widely used did not need to support longer names.

The limits in C99 were the same as in C11.

like image 122
Jonathan Leffler Avatar answered Nov 15 '22 13:11

Jonathan Leffler