liveBook · Manning

Label

As explained in Modern C, Third Edition

A label is an identifier followed by a colon that marks a statement as a target for a goto statement.

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

How Modern C, Third Edition covers it

How deep it goes
Explains syntax, same-function transfer, lifetime effects, and safety constraints, but stops short of long jumps and concurrency.
Examples
Uses RETRY, NEW_LINE, ASCEND, and a compound-literal example to contrast loop and goto repetition.
What it assumes
Assumes familiarity with C control flow and automatic-object lifetime while introducing labels alongside goto.
What you can do after
Apply it via goto for constrained cleanup or algorithmic transfers while checking object scope and lifetime.
How it connects
Connects labels to goto, control flow, scope, automatic objects, compound literals, object lifetime, and error handling.

Also explained in

Compare how 3 books explain label →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.