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.

CategoryControl flow
KindKeyword / Statement
C standardC89 / C90

📝 Syntax

goto cleanup;  /* cleanup: ... */

▶ Example

Edit the C code below and click Run; the server compiles and executes it:

Output:

status = -1

📚 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏