1

I am conventionally a Java programmer learning to program in C. Now in the Chapter 2 of the book by Kernighan and Ritchie, this is what is written.

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 uniqueness only for 6 characters and a single case.

But then they write

We tend to use short names for local variables, especially loop indices, and longer names for external variables.

Isn't it the exact opposite of what should be done. While I understand we don't want long descriptive variable names for indices, don't the above two sentences contradict each other?

aa8y
  • 3,854
  • 4
  • 37
  • 62

1 Answers1

1

K&R book was never updated to recent standards, such as C99 and current C11. The limit for external identifiers has been raised up to 31 characters. As by C11 §5.2.4.1/p1:

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

Beside that even C89 Standard recommends to get rid of arbitrary limits whenever possible. The comment in 5.2.4.1 says:

Implementation should avoid imposing fixed translation limits whenever possible.

Though comments are informative-only, it was clearly a hint for implementers to allow longer identifiers.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • So what should I read after I am done with the K&R book? We have to follow C99 for our assignments. – aa8y Feb 07 '15 at 21:55
  • @aa8y: I would highly recommend K.N. King's "C Programming: A Modern Approach" for learning C99, but it's only my personal opinion. See [this question](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) for more references. – Grzegorz Szpetkowski Feb 07 '15 at 22:45