liveBook · Manning

Goto

As explained in Modern C

goto is a C control-flow statement that transfers execution to a labeled statement within the same function.

goto transfers execution to a labeled statement within the same function. Labels mark destinations; the jump interrupts ordinary control flow and can repeat a surrounding block. section 17.3 section 13.3

A repeated block

size_t* ip = 0; RETRY: ip = &(size_t){ fun() }; if (condition) goto RETRY; printf("i is %d", *ip); The label makes RETRY a local destination. Unlike an equivalent loop, the compound literal's surrounding block keeps the object alive, so the final access is valid; the trade-off is that the resource remains occupied until the block ends. section 17.3

Use goto cautiously:

  • Reserve it for exceptional control-flow changes, especially transitional errors requiring local cleanup. section 17.3
  • Distinguish scope from lifetime: jumping around a definition can interact subtly with automatic objects and compound literals. section 13.3
  • Recognize algorithmic uses as well: a recursive descent parser uses short jumps to restart output handling or ascend. section 17.1
1   size_t* ip = 0
2   while(something)
3     ip = &(size_t){ fun() };        /* Life ends with while    */
4                                     /* Good: resource is freed */
5   printf("i is %d", *ip)            /* Bad: object is dead     */


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":1}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":2,\"ch\":13},{\"line\":2,\"ch\":19}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":1}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":9}],[{\"line\":2,\"ch\":56},{\"line\":2,\"ch\":61}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":1}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":2,\"ch\":13},{\"line\":2,\"ch\":19}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":1}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":1}],[{\"line\":4,\"ch\":4},{\"line\":4,\"ch\":10}]]"}
!@%STYLE%@!

Compare loop repetition with labeled repetition to inspect how object lifetime and later pointer access differ. section 17.3

How Modern C covers it

How deep it goes
Explains labels, same-function transfer, object lifetime, and scope interactions, while deferring fuller automatic-object rules.
Examples
Uses compound-literal retry examples, a contrived labeled jump, and a recursive descent parser for brace-based code indentation.
What it assumes
Assumes C control flow and local-object scope, while introducing labels, goto, and their lifetime consequences in context.
What you can do after
Apply it to exceptional cleanup and compact algorithmic control flow while checking object lifetime and pointer validity.
How it connects
Connects control flow with labels, scope, object lifetime, local cleanup, recursive descent parsing, longjmp, and interrupt handling.

Also explained in

Compare how 2 books explain goto →

Related concepts

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