- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- 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 - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C goto statement
![]() Share with a Friend |
C Programming - C goto statement
C goto Statement
The goto statement in C is used to transfer control to another part of the program, specifically to a labeled statement. When the goto statement is executed, the control of the program jumps directly to the location of the specified label, bypassing any intermediate code.
Syntax of goto Statement:
C
goto label;
...
label:
// Code to be executed when control jumps to this label
- The goto statement causes the program to jump to the label specified after the goto keyword. Labels are identifiers followed by a colon (:).
- The control will be transferred directly to the location of the label in the program.
Example of goto Statement:
C
#include <stdio.h>
int main() {
int i = 1;
// Use of goto to jump to a specific label
start:
if (i > 5) {
goto end; // Jump to the end label if i is greater than 5
}
printf("%d ", i);
i++;
goto start; // Jump to the start label
end:
printf("\nDone!");
return 0;
}
Explanation:
- The goto statement is used to jump to the start label, creating a loop-like behavior.
- If the value of i exceeds 5, the goto statement transfers control to the end label, where the program prints "Done!" and exits.
Output:
1 2 3 4 5 Done!
Use Cases of goto Statement:
- Looping and skipping code: You can use goto to create a loop or skip a portion of the code. However, this is not recommended because structured loops like for, while, or do-while are more readable and maintainable.
- Error handling: In some complex programs, goto can be used for error handling, allowing the program to jump to a specific part of the code where resources can be cleaned up or errors can be processed.
Example 2: Using goto for Error Handling
C
#include <stdio.h>
int main() {
int num = 10;
if (num < 0) {
goto error; // Jump to error handling section if num is negative
}
printf("Number is positive.\n");
// Jump to end if no error
goto end;
error:
printf("Error: Negative number encountered!\n");
end:
return 0;
}
Explanation:
- If num is negative, the control jumps to the error label, where an error message is printed.
- If there is no error, the program continues and prints a success message, then jumps to the end label.
Output:
Number is positive.
Disadvantages of Using goto :
- Hard to read and maintain: Excessive use of goto makes code harder to understand and maintain, as the flow of control becomes difficult to follow.
- Non-structured control flow: It bypasses the structured control flow mechanisms like loops and conditional statements, which can lead to spaghetti code.
- Reduced modularity: Using goto makes it harder to decompose your program into well-defined functions and modules.
Best Practices Regarding goto :
- Avoid excessive use: Use goto only when necessary, such as in specific error handling or complex conditional flows.
- Use structured programming constructs: Where possible, prefer using loops, functions, and conditional statements instead of goto.
Summary of goto Statement:
- The goto statement allows for an unconditional jump to a labeled statement elsewhere in the program.
- It is generally used for error handling or when structured programming constructs are not sufficient for a specific task.
- Avoid using goto in favor of more structured and readable control flow mechanisms like loops (for, while), functions, and conditional statements (if, switch).
