C 中的局部和外部变量命名约定
Local and external variable naming conventions in C
我通常是一个 Java 程序员学习用 C 编程。现在在 Kernighan 和 Ritchie 的书的第 2 章中,就是这样写的。
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.
但后来他们写
We tend to use short names for local variables, especially loop indices, and longer names for external variables.
这不是和应该做的事恰恰相反吗。虽然我知道我们不希望索引的描述性变量名称太长,但上面两句话不是相互矛盾吗?
K&R 书籍从未更新到最近的标准,例如 C99 和当前的 C11。外部标识符的限制已提高到最多 31 个字符。根据 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)
除此之外,即使是 C89 标准也建议尽可能摆脱任意限制。 5.2.4.1中的注释说:
Implementation should avoid imposing fixed translation limits whenever
possible.
虽然评论仅供参考,但它显然是对实施者允许更长标识符的暗示。
我通常是一个 Java 程序员学习用 C 编程。现在在 Kernighan 和 Ritchie 的书的第 2 章中,就是这样写的。
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.
但后来他们写
We tend to use short names for local variables, especially loop indices, and longer names for external variables.
这不是和应该做的事恰恰相反吗。虽然我知道我们不希望索引的描述性变量名称太长,但上面两句话不是相互矛盾吗?
K&R 书籍从未更新到最近的标准,例如 C99 和当前的 C11。外部标识符的限制已提高到最多 31 个字符。根据 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)
除此之外,即使是 C89 标准也建议尽可能摆脱任意限制。 5.2.4.1中的注释说:
Implementation should avoid imposing fixed translation limits whenever possible.
虽然评论仅供参考,但它显然是对实施者允许更长标识符的暗示。