C goto

Summary: in this tutorial, you will learn about the C goto statement, which is an unconditional jump statement that allows you to any specified location in the program.

C gotoIntroduction to C goto statement

The goto statement is one of C’s branching, or unconditional jump, statements.

Whenever the program encounters a goto statement, the execution jumps to the location specified by the goto statement.

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:

goto label;
Code language: C++ (cpp)

In this syntax, 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:

label: statement;
Code language: C++ (cpp)

The statement can be null therefore the label statement can be as follows:

label:;
Code language: C++ (cpp)

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.

#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; }
Code language: C++ (cpp)

How it works.

  • First, prompt for a number between 0 and 10.
  • Second, iterate from 1 to 10, the entered number match with the loop counter, jump to the end label, and display a message. If the input number does not in the range (0,10), terminate the loop 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 very difficult to follow and more difficult to troubleshoot when the error occurs.

Because the break statement only exits a one-loop level, a goto statement may be used to exit a loop from a nested loop. For example:

#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; }
Code language: C++ (cpp)

Another good example of using the C goto statement is to separate the main logic from the error handling logic. See the following example:

#include <stdio.h> int main() { 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; } // handle the logic here error_handler: // call function to handler errors handle_error(error); }
Code language: C++ (cpp)

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, you learned how to use the C goto statement to jump to a specific location in the program.

Was this tutorial helpful ?