C Programming goto

In this tutorial, you will learn about goto in C programming. Goto will provide an unconditional jump from one part of the program to another part of the program. The goto statement will alter the sequence of programs. Any programming language highly discourages the use of goto. The use of goto will make the hard to understand and modify. Thus, We will have to rewrite and modify the programs including goto.

Following is the syntax of goto.


goto Label:



Label: statement


The flowchart of the goto statement is given below which will help to understand more about goto.



Now let’s move on to our first example using goto in C language. Following example will certainly make you understand more about goto statement in C.

To calculate the factorial of a given number using goto in C:


int main(){
	int number, n;
	int fact=1;
	printf("Enter a number: ");
	scanf("%d", &number);
	n = number;
		fact = fact*number;
		goto Top;
	printf("Factorial of the given number is: %d", fact);

In the above program, we used goto for calculating the factorial of a given number. Firstly, we have initialized a variable named fact with a value 1. We have used a Label named ‘Top’ in our program. Where The program control will jump to the Label ‘Top’ when the number is greater than 0. Inside the Label ‘Top’,  the program performs the activity of multiplying the number by the value of fact and update the value fact by the new product. After the completion of this activity each time, the value of the number decreases by 1. AFterward, we will use goto which will switch the program control to the Label ‘Top’ again. This process repeats until the value of the number is greater than ‘0’. The code above will display its output as below:


Enter a number: 5
Factorial of the given number is: 120

Why don’t we use goto statement?

Above all, the main goal of the programmer is to make a program which is easily understandable by their fellow programmers. But the use of goto makes the programs buggy and hard to follow. The use of goto makes the task of editing, testing and debugging difficult for programmers. It allows the programs to do bad stuff such as jumping out of the scope. Nevertheless, it can sometimes be useful to programs. For example: while breaking from the nested loops.


SHARE C Programming goto

You may also like...

Leave a Reply

Your email address will not be published.