在 C 中,是否可以始终删除未使用的标签并保持程序运行?
In C, can unused labels always be removed and keep a program working?
如果 C 源代码有一个未使用的标签,我认为它可能(当前)有用的唯一原因是如果有人将它用作 "bookmark" 以轻松找到一段代码。除非有这些原因,是否有任何技术原因导致未使用的标签可能不想被删除?换句话说,是否可以通过删除它们来破坏编译单元,以便外部编译单元可以以某种方式使用它?
编辑:我正在修改的特定代码是 C89 之前的代码,因此历史 C 行为可能会有所不同。
对于 C99、C11 标准肯定是安全的,其中明确规定:
A label name is the only kind of identifier that has function scope.
It can be used (in a goto statement) anywhere in the function in which
it appears, and is declared implicitly by its syntactic appearance
(followed by a : and a statement).
因此,如果标签在函数中未使用,您可以将其删除。
PS 在 C89 标准中我们有:
A label name is the only kind of identifier that has function scope.
It can be used (in a goto statement) anywhere in the function in which
it appears, and is declared implicitly by its syntactic appearance
(followed by a : and a statement). Label names shall be unique within
a function.
再一次,标签被限制在函数范围内,但也出现在古老的光荣书籍 K&R C 语言,第 58 页 §3.8 转到和标签,说:
A label has the same form as a variable name, and is followed by a
colon. It can be attached to any statement in the same function as the
goto. The scope of a label is the entire function.
所以你可以安全地将它们从任何地方移除。
如果 C 源代码有一个未使用的标签,我认为它可能(当前)有用的唯一原因是如果有人将它用作 "bookmark" 以轻松找到一段代码。除非有这些原因,是否有任何技术原因导致未使用的标签可能不想被删除?换句话说,是否可以通过删除它们来破坏编译单元,以便外部编译单元可以以某种方式使用它?
编辑:我正在修改的特定代码是 C89 之前的代码,因此历史 C 行为可能会有所不同。
对于 C99、C11 标准肯定是安全的,其中明确规定:
A label name is the only kind of identifier that has function scope. It can be used (in a goto statement) anywhere in the function in which it appears, and is declared implicitly by its syntactic appearance (followed by a : and a statement).
因此,如果标签在函数中未使用,您可以将其删除。
PS 在 C89 标准中我们有:
A label name is the only kind of identifier that has function scope. It can be used (in a goto statement) anywhere in the function in which it appears, and is declared implicitly by its syntactic appearance (followed by a : and a statement). Label names shall be unique within a function.
再一次,标签被限制在函数范围内,但也出现在古老的光荣书籍 K&R C 语言,第 58 页 §3.8 转到和标签,说:
A label has the same form as a variable name, and is followed by a colon. It can be attached to any statement in the same function as the goto. The scope of a label is the entire function.
所以你可以安全地将它们从任何地方移除。