goto Unconditional jumps within a function
goto Unconditional jumps within a function
goto jumps to a same-function label, useful for cleanup but easy to abuse. Variably modified scopes impose restrictions, and labels use normal identifiers.
| Category | Control flow |
|---|---|
| Kind | Keyword / Statement |
| C standard | C89 / C90 |
📝 Syntax
goto cleanup; /* cleanup: ... */
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
status = -1
🏷️ Related control-flow keywords
📚 Related Tutorials
❓ FAQ
QCan goto jump to a label in another function?
ANo. A label has meaning only within its current function, and goto cannot cross function boundaries. setjmp and longjmp form a separate nonlocal jump mechanism with different rules and risks.
QIs using goto always bad in a C project?
ANot necessarily. A one-way goto cleanup can centralize resource release and avoid duplicated cleanup across nested error paths. Arbitrary jumps forward and backward harm readability, so restrict its use and keep labels and control flow clear.
QCan a local variable be used after goto skips its initialization?
ASkipping an ordinary automatic object's initialization can leave its value indeterminate, and reading it may cause undefined behavior. Jumping into scopes involving variable-length arrays has additional constraints and should be avoided.