SO Stinks
SO Stinks

Reputation: 3416

In C, can unused labels always be removed and keep a program working?

If a C source has an unused label, the only reason I can think it might be (currently) useful is if somebody was using it as a "bookmark" to easily find a section of code. Barring such reasons, is there any technical reason why unused labels might not want to be removed? In other words, is it possible to break a compilation unit by removing them so such that external compilation units may somehow use it?

EDIT: The specific code I am modifying is pre-C89 so historical C behaviour may make a difference.

Upvotes: 5

Views: 726

Answers (1)

Frankie_C
Frankie_C

Reputation: 4877

It is surely safe for C99, C11 standards where is clearly stated that:

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

So if the label is unused inside the function you can remove it.

PS In C89 standard we have:

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.

Once again labels are confined in function scope, but also in the old glorious book K&R C-language, page 58 §3.8 Goto and labels, says:

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.

So you can remove them from everywhere safely enough.

Upvotes: 5

Related Questions