C goto Jump Statement Tutorial

In this C tutorial we learn how to use the goto statement to jump to other sections of code in our program.

We also cover why the goto statement shouldn't be used, or at least used sparingly.

What is the goto jump statement

The goto statement is used to tell the compiler to jump from one position to another.

We can use the goto statement to repeat parts of our code for a condition. We can also use goto to break multiple loops in C, which can’t be done with the break statement.

The goto statement isn’t used that often anymore since it makes code complicated and less easy to read. We include a tutorial on the goto statement only for completeness.

How to use the goto jump statement

The goto statement works in two parts. First, we tell the compiler when we want it to jump to another section and code and second, we tell the define the section of code to jump to.

Syntax:
goto sectionToGoTo;


sectionToGoTo:
	// statements

First, we define an identifier for the section we want to jump to. The compiler will find this identifier elsewhere in our code and go to it, it will then execute the code from that point on sequentially.

Example:
#include <stdio.h>

int main()
{
    for (int i = 0; i < 10; ++i)
    {
        if (i == 5)
        {
            goto jumpTo;
        }

        printf("%d\n", i);
    }

    jumpTo:

    printf("jumped to this statement on 5\n");

    return 0;
}

In the example above, we tell the compiler to jump to the jumpTo identifier when counter in the loop gets to 5.

From there, the compiler executes code as it normally would, printing the statement to the console.

Why you shouldn't use goto

As mentioned before, it makes the code complex and less easy to read. Let’s look at an example.

Example:
#include <stdio.h>

int main()
{
    int num = 0;

    while (num < 10)
    {
        two:
        ++num;
        printf("%d\n", num);

        goto one;
    }

    one:
    if (num == 5)
    {
        goto three;
    }
    else
    {
        goto two;
    }

    three:
    if (num > 4 && num < 8)
    {
        goto four;
    }

    four:
    if (num == 5)
    {
        printf("Wait where are we at this point?");
        goto one;
    }
    else
    {
        --num;
        goto two;
    }

    return 0;
}

The code in the example above is difficult to follow clearly, which can cause mistakes when writing it. In fact, on the fourth jump the code goes into an infinite loop.

There are some use cases for using goto, such as jumping out of a nested loop. If you do decide to use goto in your code, try and use it as little as possible.

Summary: Points to remember

  • The goto statement tells the compiler to jump from one piece of code to another.
  • The goto statement works in two stages.
    • First, tell the compiler where to go.
    • Second, define the place that the compiler should go to.
  • The goto statement isn’t really used anymore because it makes code complex which often leads to problems.