Goto Statement in C

Goto Statement in C Program

In this article, I am going to discuss the Goto Statement in C Program with examples. Please read our previous articles, where we discussed the Return Statement in C. At the end of this article, you will understand what is goto statement in C and when how to use it in c program with examples.

Goto Statement in C:

It is a keyword. By using this keyword we can pass the control anywhere in the program in the local scope. When we are working with the goto statement it required an identifier called a label. Any valid identifier followed by a colon is called a label. Whenever we are working with goto statement it is called unstructured control flow statement because it breaks the rule of structure programming language.

A goto statement in C programming provides an unconditional jump from the ‘goto’ to a labeled statement in the same function. The goto statement is rarely used because it makes program confusing, less readable, and complex. Also, when this is used, the control of the program won’t be easy to trace, hence it makes testing and debugging difficult.

Note − Use of goto statement is highly discouraged in any programming language because it makes it difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten to avoid them.

Goto Statement Control Flow in C

Syntax to use goto statement in C:

Syntax to use goto statement in C

Program to understand the goto statement in C:
#include <stdio.h>
int main ()
{
    int a = 10;	/* local variable definition */
    LOOP:do	/* do loop execution */
    {
      if (a == 15)
   {
      a = a + 1;	/* skip the iteration */
      goto LOOP;
   }
      printf ("value of a: %d\n", a);
      a++;
    }
    while (a < 20);
    return 0;
}

Output:

Program to understand goto statement in C

Some tricky questions related to C goto statement
Question1: What will be the output of the below program?
#include <stdio.h>
int main()
{
    printf("CTutorials ");
    printf("Welcome ");
    XYZ:
        printf("X ");
        printf("Y ");
    goto ABC;
        printf("Programming ");
    ABC:
    printf("Hello1 ");
    printf("Hello2");
    return 0;
}

Output:

Note: In order to execute the program if the label has occurred it will be executed automatically without calling also. The creation of labels is always optional, after creating the label calling the label is also optional.

In implementation whenever we need to repeat the statement “n” number of times without using loops then we can use goto statement but in goto statement, we cannot place break and continue statement.

Question2: What will be the output of the below program?
#include <stdio.h>
int main()
{
    int i = 2;
    EVEN:
        printf("%d ", i);
        i = i + 2;
        if(i <= 20)
                goto EVEN;
    return 0;
}

Output:

Question3: What will be the output of the below program?
#include <stdio.h>
int main()
{
    printf("A");
    printf("CTutorials");
    goto ABC;
        printf("WELCOME");
        printf("HELLO");
    abc:
        printf("B");
        printf("C");   
    return 0;
}

Output:

In goto statement, labels are work with the help of case sensitive i.e. upper case label and lower case label both are different.

In the next article, I am going to discuss Functions in C Program with examples. Here, in this article, I try to explain the Goto Statement in C Program with examples. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *