C programming goto statement

In this tutorial, you will learn about c programming goto statement which is used to alter the flow of program execution.

c programming goto statement

C goto statement

In C programming, goto statement is used to alter the flow of execution of the program as we wish. Use of goto is no recommended as it is not a good programming practice though it might be handy in some situations.

structure of c programming goto statement


c programming goto statement

As shown in above structure of goto statement label is an identifier which specifies the place where the flow is to be jumped and it must be followed by colon.

Good Programming Practice
The label used in above structure must be followed by a colon and for good programming practice try avoiding the use of goto statement as much as you can.

Example to illustrate the use of goto statement in C

#include <stdio.h>

int main ()
  goto a;       //instructs compiler to jump to label a
    goto c;        //instructs compiler to jump to label c
    printf("C pro");
    goto b;       //instructs compiler to jump to label b
   return 0;
}             //end of program


C programming

Explanation of the program

As soon as the compiler encounters goto statement with label 'a' the control is jumped to label 'a:' and 'C pro' is printed.

Then compiler encounters second goto statement which commands to jump to the section where label ‘b’ exist and gram is printed.

Similarly ming is printed as the compiler encounters third goto c statement. As an output ‘C programming’ is printed’.

The big problem with goto is that it obscure the flow of control. There are many other ways to get the job done without goto.

P.S: Try avoiding goto statement instead use function and looping.