Open In App

goto Statement in C

Last Updated : 03 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The goto statement in C allows the program to jump to some part of the code, giving you more control over its execution. While it can be useful in certain situations, like error handling or exiting complex loops, it’s generally not recommended because it can make the code harder to read and maintain.

Let’s take a look at an example:

C
#include <stdio.h>

int main() {
    int n = 0;  

    // If the number is zero, jump to
  	// jump_here label
    if (n == 0)
        goto jump_here;

    // This will be skipped
    printf("You entered: %d\n", n);

jump_here:
    printf("Exiting the program.\n");
    return 0;
}

Output
Exiting the program.

Explanation: In this program, a predefined value is assigned to num. If n equals 0, the program jumps to the jump_here label using the goto statement. If n is non-zero, the program prints the value. The goto statement is used to control the flow by jumping to a specific part of the program.

Syntax

Syntax1 | Syntax2
—————————-
goto label; | label:
. | .
. | .
. | .
label: | goto label;

In the above syntax, the first line tells the compiler to go to or jump to the statement marked as a label. Here, the label is a user-defined identifier that indicates the target statement. The statement immediately followed after ‘label:’ is the destination statement. The ‘label:’ can also appear before the ‘goto label;’ statement in the above syntax. 

Flowchart of goto

goto statement in C

Flowchart of goto Statement in C

The goto statement allows for jumping to specific parts of a program and understanding its function is still valuable.

Examples of goto

Below are some examples of how to use a goto statement.

Check Even or Odd Number

C
#include <stdio.h>

int main() {
    int n = 26;
  
  	if (n % 2 == 0)
      
        // jump to even
        goto even;
    else
      
        // Jump to odd
        goto odd;

even:
    printf("%d is even", n);
  	return 0;
  	
odd:
    printf("%d is odd", n);
    return 0;
}

Output
26 is even

Prints numbers from 1 to 10

C
#include <stdio.h>

int main(){
  
  	int n = 1;
  
// Label here
label:
    printf("%d ", n);
    n++;
    if (n <= 10)
      
      	// jumb back to the label
        goto label;
    return 0;
}

Output
1 2 3 4 5 6 7 8 9 10

Explanation: The goto statement is used in this program to print numbers from 1 to 10. The variable n is incremented, and the program jumps back to the label to print the next number until n exceeds 10.

Jumping Out of a Nested Loop

C
#include <stdio.h>

int main() {
    int i, j;

    for (i = 0; i < 5; i++) {
        for (j = 0; j < 5; j++) {
            if (i == 2 && j == 2) {
              
                // Break out of both loops
                goto exit_loops;  
            }
            printf("%d %d\n", i, j);
        }
    }

exit_loops:
    printf("Exited loop");
    return 0;
}

Output
0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
1 4
2 0
2 1
Exited loop

Explanation: In this example, goto is used to exit both the inner and outer loops when i == 2 and j == 2. Without goto, we would have to use flags or multiple break statements, which would be more complicated.

Error Handling Using goto

C
#include <stdio.h>

int main() {
    int num = -1; 

    // Check if num is -1
    if (num == -1) {
      
        // Jump to error handling
        goto error;  
    }

    printf("Valid input: %d\n", num);
    return 0;

error:
    printf("Error: Invalid input detected.\n");
    return -1;
}

Output
Error: Invalid input detected.

Explanation: goto is used to jump to the error label when an invalid input (e.g., -1) is entered. This allows for centralized error handling.

Conclusion

In conclusion, the goto statement in C provides a way to jump to a specific point in the program, offering a method for controlling the flow of execution. While it can simplify certain tasks, such as error handling or managing complex loops, excessive use of goto can lead to poor code readability and maintainability. It’s often recommended to use structured control flow statements like for, while, and if in place of goto for cleaner and more understandable code.



Next Article
Article Tags :
Practice Tags :

Similar Reads