Back to: C Tutorials For Beginners and Professionals
Goto Statement in C Language with Examples
In this article, I will discuss the Goto Statement in C Language with Examples. Please read our previous articles discussing the Return Statement in C Language with Examples. At the end of this article, you will understand the goto statement in C and how to use it in the C program with examples.
Goto Statement in C Language:
The goto statement in C programming is a control flow statement that allows you to jump from one part of the program to another. It transfers control to a labeled statement within the same function. It’s typically used to break out deeply nested loops or jump to error-handling code sections. The use of goto can be helpful in some scenarios but is generally discouraged as it can make the code harder to read and maintain.
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 modify. Any program that uses a goto can be rewritten to avoid them.
Flow Chart of Goto Statement:
The following diagram shows the flowchart of the goto statement. Here, as you can see, we have three labels i.e., Label 1, Label 2, and Label 3. Whenever we are executing our application code, if we have written the goto label name, for example, goto Label 3, then the control will immediately jump to the statement that is written after the Label 3 identifier.
Syntax to Use Goto Statement in C Language:
Syntax
goto label;
- goto is the keyword.
- label is an identifier that marks a position in the code to which control will jump. It’s defined elsewhere in the code, followed by a colon.
Defining a Label
A label in C is defined simply by writing an identifier followed by a colon (:) at the point in the code where you want the control flow to jump to. For example:
label_name:
// Some code here
Flow of Execution
- Normal Flow: The program executes line by line from top to bottom in a sequential manner.
- Encountering goto: When the execution reaches a goto statement, it immediately jumps to the line of code where the label is defined, skipping any intervening code.
- After Jumping: Execution resumes from the label’s location and continues sequentially from there.
Example to Understand the Goto Statement in C Language:
Here’s a basic example of how the goto statement works:
#include <stdio.h> int main() { int num = 0; start: num++; printf("num = %d\n", num); if (num < 5) goto start; printf("Finished\n"); return 0; }
In this example:
- The program defines a label start.
- The program increments the variable num and prints its value.
- If num is less than 5, the program jumps back to the start label.
- This loop continues until num is no longer less than 5.
- Once num reaches 5, the loop ends, and the program prints “Finished”.
Example: Using goto for Error Handling
#include <stdio.h> int main() { FILE *file = fopen("nonexistentfile.txt", "r"); if (file == NULL) { goto error; } // Code to read file or perform other operations fclose(file); return 0; error: perror("Error opening file"); return -1; }
In this example, goto is used for error handling. If the file cannot be opened, the program jumps to the error label, prints the error message, and exits.
Example: Exiting Nested Loops
#include <stdio.h> int main() { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (i == 3 && j == 3) { goto exit_loops; } printf("(%d, %d)\n", i, j); } } exit_loops: printf("Exited loops at i = 3 and j = 3\n"); return 0; }
This example demonstrates how goto can be used to break out of nested loops.
While goto can be used for breaking out of deeply nested loops or for error handling in some cases, it’s often better to use structured loop constructs like for, while, or do-while, and functions to keep your code clean and understandable. Excessive use of goto can lead to “spaghetti code,” which is difficult to follow and maintain.
Some Tricky Questions Related to the C Goto Statement
Question 1: What will be the output of the Program Below?
#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: To execute the program, if the label has occurred, it will be executed automatically without calling. The creation of labels is always optional. After creating the label calling the label is also optional.
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.
Question 2: What will be the output of the program below?
#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
Question 3: What will be the output of the program below?
#include <stdio.h> int main() { printf("A"); printf("CTutorials"); goto ABC; printf("WELCOME"); printf("HELLO"); abc: printf("B"); printf("C"); return 0; }
Output:
In the goto statement, labels work with the help of case sensitivity, i.e., upper case labels and lower case labels are both different.
In the next article, I will discuss Functions in C Language with examples. In this article, I try to explain the Goto Statement in C Language with examples. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, questions, or comments about this article.