0

Here's a direct quote from the Book (K&R, 2nd ed, p. 35):

"At least the first 31 characters of an internal name are significant. For function names and external variables, the number may be less than 31, because external names may be used by assemblers and loaders over which the language has no control. For external names, the standard guarantees only for 6 characters and a single case."

And in C99 there is no length limitation on its internal names, but only the first 63 are guaranteed to be significant (§5.2.4.1 Translation Limits).

My question is why are these limits specifically 31 or 63? Why this number specifically? Why not 19, 24 or any other number? If it's an implementation issue, is there a benefit from making it 31 or 63?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
HATEM EL-AZAB
  • 331
  • 1
  • 3
  • 11

1 Answers1

8

The compiler/linker writers associated with a language design committee usually impose some limits so that they can make assumptions in the implementation of the toolchain or in the accompanying binary file formats (e.g., ELF, COFF, etc). The numbers 31 and 63 were probably chosen simply because they are 2n-1 and programmers like 2n for some silly reason. The -1 is usually to account for either a leading _ or a trailing NUL character in the name.

D.Shawley
  • 58,213
  • 10
  • 98
  • 113
  • what is meant by toolchain,and about 2n-1 can give an example i'm confused – HATEM EL-AZAB Apr 13 '12 at 13:26
  • 2
    Since it's C, a trailing \0 is a safe bet. @HATEMEL-AZAB: "Toolchain" usually refers to the sequence of tools used to build a C program: preprocessor, compiler, linker. – MSalters Apr 13 '12 at 14:10
  • Usually naming conventions at the compiler <-> linker level are based on mangling so I would suspect the leading underscore assumption myself. The names included in the binary symbol tables are not usually NULL-terminated. They are padded to a fixed length. In any case, it's just a convenient limit. – D.Shawley Apr 13 '12 at 14:22