Goto Statement in C

Updated

Prerequisite – Control Statements in C
The goto statement in C allows you to jump to a different point in the program’s code. It’s often considered to be a controversial feature of the language, as it can lead to unreadable and difficult-to-debug code.

Syntax:
The basic syntax of the goto statement in C is as follows:

goto label;

// ...

label: statement;

Here, label is an identifier that you’ve defined in your program’s code, and statement is a statement that you want to execute when the goto statement is encountered. When the goto statement is executed, the program will jump to the location in the code where the corresponding label appears, and execution will continue from there.

Example:
Here’s an example of using a goto statement in C:

#include <stdio.h>

int main() {
    int i = 1;

    loop:
    printf("%d\n", i);
    i++;

    if (i <= 10) {
        goto loop;
    }

    return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10

In this example, the goto statement is used to create a loop that prints the numbers from 1 to 10. The loop label is defined immediately before the printf statement, and the if statement is used to check if the value of i is less than or equal to 10. If i is less than or equal to 10, the goto statement is executed, and the program jumps back to the loop label. This loop will continue until the value of i is greater than 10.

While the goto statement can be useful in some cases, it’s generally recommended to avoid using it in your code, as it can make the code harder to read and debug. Instead, it’s often better to use loops and conditional statements to control the flow of your program.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Comment
Next tutorial
Mithlesh Upadhyay Published 20 Feb 2023 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials

  • Call by value, call by address and call by reference in C++

    The parameters of the function call are known as actual parameters and the parameters of the function definition are known as formal parameters. There are 3 ways of…

    3 min read
  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. GATE CS 2027 Online Test Series72 tests in the real GATE…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read