- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Goto Statement in C
What is goto Statement in C?
The goto statement is used to transfer the program's control to a defined label within the same function. It is an unconditional jump statement that can transfer control forward or backward.
The goto keyword is followed by a label. When executed, the program control is redirected to the statement following the label.If the label points to any of the earlier statements in a code, it constitutes a loop. On the other hand, if the label refers to a further step, it is equivalent to a Jump.
goto Statement Syntax
The syntax of goto statement is −
goto label; . . . . . . label: statement;
The label is any valid identifier in C. A label must contain alphanumeric characters along with the underscore symbol (_). As in case of any identifier, the same label cannot be specified more than once in a program. It is always followed by a colon (:) symbol. The statement after this colon is executed when goto redirects the program here.
goto Statement Flowchart
The following flowchart represents how the goto statement works −
goto Statement Examples
Example 1
In the following program, the control jumps to a given label which is after the current statement. It prints a given number, before printing the end of the program message. If it is "0", it jumps over to the printf statement, displaying the message.
#include <stdio.h> int main (){ int n = 0; if (n == 0) goto end; printf("The number is: %d", n); end: printf ("End of program"); return 0; }
Output
Run the code and check its output −
End of program
Example 2
Here is a program to check if a given number is even or odd. Observe how we used the goto statement in this program −
#include <stdio.h> int main (){ int i = 11; if (i % 2 == 0){ EVEN: printf("The number is even \n"); goto END; } else{ ODD: printf("The number is odd \n"); } END: printf("End of program"); return 0; }
Output
Since the given number is 11, it will produce the following output −
The number is odd End of program
Change the number and check the output for different numbers.
Example 3
If goto appears unconditionally and it jumps backwards, an infinite loop is created.
#include <stdio.h> int main (){ START: printf("Hello World \n"); printf("How are you? \n"); goto START; return 0; }
Output
Run the code and check its output −
Hello World How are you? ....... .......
The program prints the two strings continuously until forcibly stopped.
Example 4
In this program, we have two goto statements. The second goto statement forms a loop because it makes a backward jump. The other goto statement jumps out of the loop when the condition is reached.
#include <stdio.h> int main(){ int i = 0; START: i++; printf("i: %d\n", i); if (i == 5) goto END; goto START; END: printf("End of loop"); return 0; }
Output
Run the code and check its output −
i: 1 i: 2 i: 3 i: 4 i: 5 End of loop
Example 5
The goto statement is used here to skip all the values of a looping variable that matches with that of others. As a result, all the unique combinations of 1, 2 and 3 are obtained.
#include <stdio.h> int main (){ int i, j, k; for(i = 1; i <= 3; i++){ for(j = 1; j <= 3; j++){ if (i == j) goto label1; for (k = 1; k <= 3; k++){ if (k == j || k == i) goto label2; printf("%d %d %d \n", i,j,k); label2: ; } label1: ; } } return 0; }
Output
Run the code and check its output −
1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
Avoid Using the goto Statement in C
Note that goto in C is considered unstructured, as it allows the program to jump to any location in the code, it can make the code hard to understand, follow, and maintain. Too many goto statements sending the program control back and forth can make the program logic difficult to understand.
Noted computer scientist dsger Dijkstra recommended that goto be removed from all the programming languages. He observed that if the program control jumps in the middle of a loop, it may yield unpredictable behaviour. The goto statements can be used to create programs that have multiple entry and exit points, which can make it difficult to track the flow of control of the program.
Dijkstra's strong observations against the use of goto statement have been influential, as many mainstream languages do not support goto statements. However, it is still available in some languages, such as C and C++.
In general, it is best to avoid using goto statements in C. You can instead effectively use if-else statements, loops and loop controls, function and subroutine calls, and try-catch-throw statements. Use goto if and only if these alternatives don’t fulfil the needs of your algorithm.