The goto statement in C is an unconditional jump that transfers program control directly to a labelled statement within the same function. While modern C style discourages it, goto is still useful in a few real situations — most commonly for breaking out of deeply nested loops and for centralised cleanup/error handling. In this tutorial you will learn the syntax of goto, see a clean, working example, and understand when (and when not) to use it.
Syntax of goto
The goto statement needs a label — an identifier followed by a colon — somewhere in the same function:
goto label;
...
label:
statement;
When execution reaches goto label;, control jumps straight to label: and continues from there.
Example: Guarding Against Divide-by-Zero
A classic teaching example is to use goto to skip a division when the divisor is zero, jumping to a single exit point. Here is a clean, portable version that compiles on any standard C compiler:
#include <stdio.h>
int main(void)
{
double num1, num2;
printf("Enter the first number : ");
if (scanf("%lf", &num1) != 1) {
printf("Invalid input.\n");
return 1;
}
printf("Enter the second number : ");
if (scanf("%lf", &num2) != 1) {
printf("Invalid input.\n");
return 1;
}
/* Use goto to skip the division when the divisor is zero. */
if (num2 == 0.0)
goto divide_error;
printf("%.2f divided by %.2f is %.2f\n", num1, num2, num1 / num2);
return 0;
divide_error:
printf("Error: cannot divide by zero.\n");
return 1;
}
How the Program Works
- The program reads two numbers using
scanf. The %lf format specifier reads a double, and we check its return value to make sure the input was valid.
- If the second number is
0.0, the condition if (num2 == 0.0) is true and goto divide_error; jumps past the division straight to the divide_error: label.
- Otherwise the division is performed and the result is printed, and the program returns before ever reaching the label.
- The
divide_error: label acts as a single, clearly named exit point for the error case.
Notice that this modern version replaces the old, unsafe gets() call from textbook examples with scanf and input validation — gets() was removed from the C standard in C11 because it can overflow buffers.
Sample Output
Enter the first number : 22
Enter the second number : 7
22.00 divided by 7.00 is 3.14
Enter the first number : 10
Enter the second number : 0
Error: cannot divide by zero.
When Should You Use goto?
| Situation |
Is goto a good idea? |
| Breaking out of deeply nested loops |
Acceptable — often cleaner than multiple flag variables |
| Centralised error cleanup (freeing resources) |
Common in C, used heavily in the Linux kernel |
| Ordinary loops and branching |
No — use for, while, if/else instead |
| Jumping backwards to create a loop |
No — it produces hard-to-read “spaghetti code” |
As a rule of thumb: prefer structured control flow (for, while, break, continue) and reach for goto only for forward jumps to a single cleanup point. For a deeper discussion of control flow and good C style, The C Programming Language by Kernighan and Ritchie remains the definitive reference — see K&R on Amazon.
This post contains affiliate links. If you buy through them, we may earn a small commission at no extra cost to you.
Related C Programs
If you don’t want to install a compiler just to try this out, you can paste the code straight into one of the best online C compilers and run it in your browser. When you’re ready for a proper local setup, follow our guide to building a complete C development environment.