Goto Statement in C with example

Goto Statement in C with example

In this tutorial We will learn how to use goto statement in C Programming Language with the help of an examples.

In C programming language, goto is a special type of statement which we can use when we want to transfer the control of execution at some another label.

The goto statement required a label in order to identify the place where the branch is to be transfered.

This Label can be any valid variable name. And this Label must be followed by a colon and it is placed just before the statement where the control of goto is to be transferred.

Syntax of goto:

goto label;
... .. ...
... .. ...
label: 
statement;

Example 1: C goto statement example

[c] #include<stdio.h> #include<conio.h> #include<stdlib.h> int main() { int n; printf("Enter the no"); scanf("%d", &amp;n); if(n&lt;=10) goto no; else { printf("\n No. is greater than the 10"); printf("\n Enjoy the C Language."); exit(0); } no: printf("No is smaller than 10"); getch(); return 0; } [/c]

Output

Let see the explanation of the above goto C Program

The above Program is a basic example to clear the concept of C go to statement. In the above program, you can see that there is a variable n of integer type. Now, what the above program will do?

It will check the condition with the help of if statement. If the condition will satisfy then it will transfer the control at label no, as the property of goto statement of C and logic of the above program.

Now when we will give any input less then or equal to 10 then it will satisfy the if condition. And if the condition will satisfy then if body will execute and control will transfer to the label “no”.

Now after insertion in the body it will transfer to label no as mentioned goto no.

So it will print “no is smaller than 10” as output. That is all about the goto statement in the C programming language. Basically it will used by the programmers very rarely and also this statement is not that much popular as per real world solutions.

Now I hope your concept is now cleared 🙂