← Back to C Course | Chapter 14: Additional Topics | Lesson 1 of 9

C goto Statement

goto jumps straight to another spot in the code marked by a label, like skipping to a different page of a book. It can make code hard to follow, so it is used sparingly.
Syntax
c
goto label;
// ... skipped code ...
label:
    statement;

What is goto?

goto transfers execution directly to a labeled line elsewhere in the same function, bypassing the normal sequential flow of the code entirely, which makes it powerful but also the control-flow tool most likely to make a program's logic hard to follow if used carelessly.

Example: What is goto?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	goto skip;
	// Print formatted text to the screen
	printf("never runs");
	skip:
	printf("Jumped past the printf");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Syntax and Labels

A label is simply an identifier followed by a colon placed in front of a statement, marking that line as a valid jump target, and it can be positioned anywhere within the same function that a goto elsewhere in that function wants to reach.

Example: Syntax and Labels

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	if (x > 0) goto positive;
	// Print formatted text to the screen
	printf("not positive");
	// Return 0 to signal that the program finished successfully
	return 0;
	positive:
	printf("Positive");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Breaking Out of Deep Nesting

When an error condition is detected deep inside several nested loops, a single goto cleanup; can exit all of them in one step, whereas a plain break only escapes the single innermost loop, leaving you to manually break out of each remaining level.

Example: Breaking Out of Deep Nesting

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		// Loop: repeat while the condition holds
		for (int j = 0; j < 3; j++) {
			// Check whether i == 1 && j == 1
			if (i == 1 && j == 1) {
				goto cleanup;
			}
		}
	}
	cleanup:
	printf("Exited both loops");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Backward Jumps

Because goto can jump to a label that appears earlier in the code, it's possible to build a loop by jumping backward to a label — but you must make sure the surrounding condition eventually evaluates false, or that backward jump repeats forever exactly like a runaway while loop.

Example: Backward Jumps

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare i and set it to 0
	int i = 0;
	loopStart:
	if (i < 3) {
		// Print formatted text to the screen
		printf("%d ", i);
		i++;
		goto loopStart;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Best Practices with goto

Most C style guides restrict goto to a narrow set of legitimate uses, chiefly escaping deeply nested loops and centralizing cleanup code (like closing several open resources before returning), while steering clear of using it as a general substitute for structured loops and conditionals.

Example: Best Practices with goto

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare success and set it to 0
	int success = 0;
	// Check whether !success
	if (!success) {
		goto cleanup;
	}
	// Print formatted text to the screen
	printf("never runs");
	cleanup:
	printf("Centralized cleanup reached");
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using goto to jump into the middle of a block or past a variable initialization, which can be invalid or unsafe.
  2. Overusing goto, which produces tangled code that is hard to follow.
  3. Misspelling a label or forgetting the colon after it.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.