goto statement in C

goto is a jump statement used to transfer program control unconditionally from one part of a function to another. I have used the word unconditionally because there is no restriction on control transfer. You can transfer program control from one position to any position within a function. Many programmers uses goto to gain full control on their program.

Syntax of goto statement

label: 
goto label;

Parts of goto statement

goto is divided in two parts, label definition and goto keyword.

  • Label is a valid C identifier followed by colon symbol :. Label specifies control transfer location. Each label work as a bookmark to specific location. You are free to define any number of labels anywhere inside a function.
  • goto is a keyword used along with label name to transfer program control to the mentioned label. You can only transfer program control to a label within same function. Cross function control transfer, using goto is not possible and results in compilation error.

There is no restriction in order of goto and label. You are free to define a label anywhere inside a function. For example both the below examples are valid.

Label definition above goto statement

label1: 
goto label1;

Label definition below goto statement

goto label2;
label2:

Flowchart of goto statement

goto statement flowchart

Use of goto in programming

goto makes program less readable, error-prone and hard to find bugs. Due to the unconditional control transfer from one part to other part of a function. At one point, you as a programmer cannot tell how program control came to a certain point in your code. Therefore, most of the programmers avoid the usage of goto.

Important note: You should avoid usage of goto as much as possible. Try to replace goto statements with a combination of if…else, switch…case and loopping statements.

Consider below example of goto to demonstrate when and why you should avoid usage of goto.

#include <stdio.h>

int main()
{
    int i=1;

    start:
        goto print;

    print:
        printf("%d ", i);
        goto next;

    increment:
        i++;
        goto print;

    next:
        if(i < 10)
            goto increment;
        else
            goto exit;

    printf("I cannot execute.");

    exit:
        return 0;
}

The above program is hard to read and find bugs if any. Hence, you must avoid usage of goto in such situations.

Okay lot of programmers including me talks about the unreliability of goto. However, goto can be useful in many cases such as.

  • Moving out of a deeply nested loop.
  • Transferring control to any nested loop level from other.

Note: You can construct any loop using combination of goto and if...else.

Example program to demonstrate goto statement

Many text and other materials on internet make use of goto as a loop. However, I believe goto should not be used as a replacement of loops. In below program I will show one real use of goto statement.

As I spoke earlier you can make use of goto to exit from a deeply nested loop. In below program I will show how to get out of a deeply nested loop using goto statement.

/**
 * C program to demonstrate usage of goto
 */

#include <stdio.h>

int main()
{
    /* Variable declaration */
    int i, j, k;

    /* Some sample loop */
    for(i=1; i<=10; i++)
    {
        for(j=1; j<=10; j++)
        {
            k = 1;
            while(k<=10)
            {
                /* Some condition */
                if(j==5 && k==5)
                {
                    /* Move the program control outside the loop */
                    goto out_of_loop;
                }
                printf("%d ", k);
                k++;
            }
        }
    }
    /* goto label */
    out_of_loop:

    return 0;
}