Understanding the Goto Statement in C Programming

The goto statement in C allows for an unconditional transfer of control to a defined label within the same function. This versatile jump statement can redirect program flow either forward or backward, enabling complex control structures. By following the goto keyword with a label, you can create loops or jumps in your code, making it essential for specific programming scenarios, though its use should be approached with caution for maintainability.



Goto Statement in C

The goto statement is used to transfer the program's control to a defined label within the same function. It is an unconditional jump statement that can transfer control forward or backward.

The goto keyword is followed by a label. When executed, the program control is redirected to the statement following the label. If the label points to any of the earlier statements in a code, it constitutes a loop. On the other hand, if the label refers to a further step, it is equivalent to a jump.

goto Statement Syntax


goto label;
...
label: statement;

The label is any valid identifier in C. A label must contain alphanumeric characters along with the underscore symbol (_). As in the case of any identifier, the same label cannot be specified more than once in a program. It is always followed by a colon (:) symbol. The statement after this colon is executed when goto redirects the program here.

goto Statement Flowchart

goto Statement Examples

Example 1

In the following program, the control jumps to a given label which is after the current statement. It prints a given number before printing the end of the program message. If it is "0", it jumps over to the printf statement, displaying the message.


#include 

int main() {
    int n = 0;

    if (n == 0)
        goto end;
    printf("The number is: %d", n);

end:
    printf("End of program");

    return 0;
}
Output

End of program

Example 2

Here is a program to check if a given number is even or odd. Observe how we used the goto statement in this program.


#include 

int main() {
    int i = 11;
    if (i % 2 == 0) {
    EVEN:
        printf("The number is even \n");
        goto END;
    } else {
    ODD:
        printf("The number is odd \n");
    }
END:
    printf("End of program");

    return 0;
}
Output

The number is odd
End of program

Example 3

If goto appears unconditionally and it jumps backwards, an infinite loop is created.


#include 

int main() {
START:
    printf("Hello World \n");
    printf("How are you? \n");
    goto START;

    return 0;
}
Output

Hello World
How are you?
.......
.......

Example 4

In this program, we have two goto statements. The second goto statement forms a loop because it makes a backward jump. The other goto statement jumps out of the loop when the condition is reached.


#include 

int main() {
    int i = 0;
START:
    i++;
    printf("i: %d\n", i);
    if (i == 5)
        goto END;
    goto START;
END:
    printf("End of loop");

    return 0;
}
Output

i: 1
i: 2
i: 3
i: 4
i: 5
End of loop

Example 5

The goto statement is used here to skip all the values of a looping variable that matches with that of others. As a result, all the unique combinations of 1, 2, and 3 are obtained.


#include 

int main() {
    int i, j, k;

    for (i = 1; i <= 3; i++) {
        for (j = 1; j <= 3; j++) {
            if (i == j)
                goto label1;

            for (k = 1; k <= 3; k++) {
                if (k == j || k == i)
                    goto label2;

                printf("%d %d %d \n", i, j, k);

            label2: ;
            }
        label1: ;
        }
    }
    return 0;
}
Output

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Avoid Using the goto Statement in C

Note that goto in C is considered unstructured, as it allows the program to jump to any location in the code, it can make the code hard to understand, follow, and maintain. Too many goto statements sending the program control back and forth can make the program logic difficult to understand.

Noted computer scientist Edsger Dijkstra recommended that goto be removed from all programming languages. He observed that if the program control jumps in the middle of a loop, it may yield unpredictable behavior. The goto statements can be used to create programs that have multiple entry and exit points, which can make it difficult to track the flow of control of the program.

Dijkstra's strong observations against the use of the goto statement have been influential, as many mainstream languages do not support goto statements. However, it is still available in some languages, such as C and C++.

In general, it is best to avoid using goto statements in C. You can instead effectively use if-else statements, loops and loop controls, function and subroutine calls, and try-catch-throw statements. Use goto if and only if these alternatives don’t fulfill the needs of your algorithm.