Goto Statement in C

Goto Statement in C Language:

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 a goto statement it is called an 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 the 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 − The use of the 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 in C Language with Examples
Syntax to use goto statement in C Language:
Syntax to use goto statement in C Language
Example to Understand the goto statement in C Language:
#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:
Example to Understand the goto statement in C Language
Some tricky questions related to the 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: CTutorials Welcome X Y Hello1 Hello2

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 the goto statement but in the goto statement, we cannot place the 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: 2 4 6 8 10 12 14 16 18 20

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:
Goto Statement in C Program with Examples

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

Follow Us On

Leave a Reply

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