Summary: in this tutorial, you will learn about C goto statement, which is an unconditional jump statement that allows you to any specified location in the program.
Introduction to C goto statement
The goto
statement is one of C’s branching, or unconditional jump, statements. Whenever the program reaches a goto
statement, the execution jumps to the location specified by the goto
statement. We call the goto
statement is an unconditional statement because whenever the program encounters a goto
statement, the execution of the program branches accordingly, which does not depend on any condition like the if statement.
The following illustrates the syntax of the goto
statement:
1 | goto label; |
The label
specified after the goto
statement is the location where the execution is going to branch. The label
statement starts with an identifier, and then a colon ( :
), and then a statement, and ends with a semicolon ( ;
) as follows:
1 | label: statement; |
The statement can be null therefore the label
statement can be as follows:
1 | label:; |
A goto statement and its target, or label, can be in a different block but they are must be in the same function.
C goto statement example
The following example illustrates how to use the goto
statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <stdio.h> #define MAX 10 int main() { int needle; /* get input from user*/ printf("Please enter a number (0-10):"); scanf("%d",&needle); int i; for(i = 0; i < MAX;i++) { if(i == needle) { goto end; } else { printf("Current number %d\n",i); } } printf("Loop terminated normally."); end: printf("Jumped from the goto statement\n"); return 0; } |
How it works.
- First, we ask the user to enter a number between 0 and 10.
- Second, we loop from 1 to 10, if we found the input number, we jump to the
end
label and display a message. If the user enters a number that is not in the range of (0,10) the loop is terminated normally.
When to use C goto statement
It is good practice to use the break
, continue
and return
statement instead of the goto
statement whenever possible. If the goto
statement is overused, it makes the code unreadable and more difficult to troubleshoot when the error occurs.
Because the break
statement only exits one loop level, a goto
statement may be used to exit a loop from a nested loop. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <stdio.h> #define MAX 10 int main() { int i, j; for(i = 0; i < MAX;i++) { for(j = 0; j < MAX; j++) { /* process something here*/ goto stop; } } stop:; return 0; } |
Another good example of using the C goto
statement is to separate the main logic from the error handling logic. See the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | void f() { int error; /* put the logic here */ if( /*error */){ error = 1; goto error_handler; } /* put the logic here */ if( /*error */){ error = 2; goto error_handler; } /* put the logic here */ if( /*error */){ error = 3; goto error_handler; } error_handler: handle_errors(error); /* call function to handler errors*/ ; } |
In the f()
function above, instead of handling errors in various parts of the function, we use the goto
statement and the handle_errors()
function to separate the logic.
In this tutorial, we have shown you how C goto
statement works and when to apply it in your programming context.