A label is an identifier followed by a colon that marks a statement as a target for a goto statement. The goto transfers execution to that label within the same function. Labels therefore provide short, non-structured changes in control flow and should generally be reserved for exceptional cases such as local cleanup or specific algorithmic conditions. section 19.0 section 19.3
Repeating execution at a label
RETRY: ip = &(size_t){ fun() }; if (condition) goto RETRY; printf("i is %d", *ip); section 19.3
A label can change more than the visible order of statements: jumping back to it can alter the lifetime of local objects. An object defined inside a loop receives a new instance on each iteration and may die when that iteration's block ends. An object defined in a surrounding block can remain alive across repeated execution, potentially retaining resources longer than necessary. section 19.3
TAKEAWAY rules:
- Use goto and labels only for exceptional control-flow changes, commonly transitional error cleanup or a specific algorithmic condition. section 19.3
- Track scope and lifetime when a jump enters code that defines or uses automatic objects. section 13.3
size_t* ip = nullptr;
RETRY:
ip = &(size_t){ fun() }; /* Life continues */
if (condition) goto RETRY;
/* Bad: resource is kept */
printf("i is %d", *ip) /* Good: object is alive */
Compare a loop with a labeled retry: the loop releases its compound literal after each iteration, while the repeated labeled block keeps the object alive until the surrounding block ends. section 19.3


