Mastering the C Goto Statement: Syntax, Practical Use, and Best Practices
Mastering the C Goto Statement
In this guide you’ll learn how the goto statement works in C, its proper syntax, when it can be handy, and why it’s generally discouraged in modern code.
Syntax of the Goto Statement
goto label;
...
label:
statement;
A label is an identifier followed by a colon. When the goto statement is executed, control jumps directly to the line marked by that label.

Example: Using Goto to Exit a Loop Early
#include <stdio.h>
int main() {
const int maxInput = 100;
int i;
double number, average, sum = 0.0;
for (i = 1; i <= maxInput; ++i) {
printf("%d. Enter a number: ", i);
scanf("%lf", &number);
/* If the user enters a negative number, skip the rest of the loop */
if (number < 0.0) {
goto exit_loop;
}
sum += number;
}
exit_loop:
average = sum / (i - 1);
printf("Sum = %.2f\n", sum);
printf("Average = %.2f", average);
return 0;
}
Output
1. Enter a number: 3 2. Enter a number: 4.3 3. Enter a number: 9.3 4. Enter a number: -2.9 Sum = 16.60 Average = 5.53
Why Most Developers Avoid Goto
The goto statement can create “spaghetti code” that’s difficult to read, test, and maintain. For example:
one:
for (i = 0; i < number; ++i) {
test += i;
goto two;
}
two:
if (test > 5) {
goto three;
}
// ...
Jumping out of nested scopes or across complex control structures often hides program flow, leading to bugs that are hard to diagnose.
When Goto Might Still Be Appropriate
While rarely needed, goto can simplify specific scenarios, such as:
- Breaking out of deeply nested loops without multiple flags.
- Centralizing cleanup code before exiting a function.
Use it sparingly, and always document the intention so future maintainers understand the jump’s purpose.
Should You Use Goto?
In most modern C programs, structured control flow constructs—loops, functions, and switch statements—cover the use cases for goto. The C standard and best‑practice guidelines recommend avoiding it unless there’s a compelling reason. As Bjarne Stroustrup, creator of C++, famously noted: "The fact that 'goto' can do anything is exactly why we don't use it."
Ultimately, if you can achieve the same logic with clear, structured code, choose that path. Reserve goto for rare, well‑justified situations and document it thoroughly.
C Language
- Beeper (Pager) Technology: From Early Innovations to Tomorrow’s Two‑Way Communication
- Mastering Conditional Logic in C#: If, If‑Else, If‑ElseIf, and Nested If Statements
- Mastering the C# Switch Statement: Syntax, Examples & Best Practices
- C# Break Statement: How & When to Use It
- Mastering the C# Continue Statement: How to Skip Loop Iterations Effectively
- Master C++ Conditional Statements: if, if...else, and Nested if...else Explained
- Mastering the C++ break Statement
- Mastering C++ Continue Statement: Practical Examples & Loop Control
- Mastering C Conditional Statements: If, Else, and More
- Mastering the C Switch Statement: Syntax, Flow, and Practical Example