C Programming

goto Statement in C

One of the useful statements that we can use in the C programs is the goto statement. Even though its sometimes discouraged for making code unreadable, it’s a fundamental feature of the C Language that is sometimes needed. This statement allows us to control the flow of a program by jumping to and from various parts of the program.

In this tutorial, we will learn what this statement does, how it works, and the various instances it can come in handy.

NOTE: It is good to keep in mind that the goto statement is not widely used. It tends to create some unnecessarily complex code but it can be effective, and learning about it can help you avoid its pitfalls.

goto Statements

Goto, as the name suggests, allows us to transfer the control of a program from one part to another without using any conditional statement evaluation.

It provides a form of low-level and manual control flow. Unlike the conditional statements that evaluate a condition and then perform an action, the goto statement just switches to another part of the code without any context.
As a result, it often leads to spaghetti code which is basically a code that is hard to understand due to lack of defined structure or foundation on the resulting actions. Think of a bowl and twisted spaghetti, using the goto statements incorrectly can lead to such a code that makes you want to off yourself due to lack of coherent structure in the execution.

What does goto do?

In simple terms, when the program encounters a goto statement, the program jumps to the specified label which automatically bypasses any code that is defined between the goto statement and the target label. You can use it to, for example, break out of a loop, handle the errors, or simply with complex cases of nested statements.

goto syntax

The following shows the basic syntax of the goto statement in C:

goto label;
//...
label:
// logic at the label

We start with the “goto” keyword followed by the name of the label that we wish. We then call the actual label and specify the code that we wish to run.

Example: Break Out of a Loop with goto

Let us look at a basic example that demonstrates the use of the goto statement to break out of a loop:

#include <stdio.h>

int main() {
    int i = 0;

start:

    printf("Value of i: %d\n", i);

    i++;

    if (i < 5)
        goto start;
    return 0;
}

In this example, we start by declaring a positive integer variable and initialize its value to 0. We then create a label called “start”. In C, the labels are followed by a colon to denote that they are targets for the goto statement. In the label, we print the value of “i” to the console and increment the value of “i” by 1 on each iteration.

Next, we setup a goto statement that unconditionally jumps to the “start” label. This creates a loop that jumps back to the start label as long as the value of “i” is less than 5.

Run the previous code as follows:

Value of i: 0                                                                                        
Value of i: 1                                                                                        
Value of i: 2                                                                                        
Value of i: 3                                                                                        
Value of i: 4

It is good to keep in mind that if you want to deal with loops in a more effective and efficient manner, use other constructs such as the “for” and “while” loops. Unless you closely examine the given code, you can hardly tell that there is a loop in the code.

Conclusion

In this tutorial, we explored the fundamentals and workings of the goto statements in C/C++, how it works, and how to use it. We strongly recommend to avoid using the goto statements unless you explicitly need to.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list