Skip to content
StudyFame

goto Statement in C

The goto statement makes the program jump straight to another line, marked with a label. It is the oldest and simplest way to change the flow of a program.

In this lesson you will learn how goto works, see two small examples, and understand why modern programmers avoid it in most cases, a point examiners love to ask about.

What You Will Learn

  • What goto and labels are.
  • The syntax of goto.
  • Forward and backward jumps.
  • Why goto makes code hard to read ("spaghetti code").
  • The few cases where goto is acceptable.

goto Statement in C Explained

Exam definition: The goto statement is an unconditional jump statement that transfers control to a labelled statement in the same function.

  • A label is a name followed by a colon, like again:. It marks a place in the code.
  • goto again; jumps to that place, no conditions asked.
  • The label must be in the same function as the goto.

Memory trick: goto is like teleporting in a game: you vanish here and appear at the label. Fun once, but if everyone teleports everywhere, nobody can follow the story. That's why programs full of goto are called "spaghetti code": tangled like noodles.

Syntax of goto in C

goto label_name;      /* jump */
...
label_name:           /* the place to jump to */
    statement;

A jump can go forward (label below the goto) or backward (label above the goto). A backward jump repeats code, like a loop.

Example 1: Print 1 to 5 Using goto (Backward Jump)

#include <stdio.h>

int main(void)
{
    int i = 1;

start:
    printf("%d ", i);
    i++;
    if (i <= 5)
        goto start;      /* jump back to the label */

    printf("\nDone\n");
    return 0;
}

Output

1 2 3 4 5 
Done

Code Explanation

  1. start: is a label. It does nothing by itself; it just marks the line.
  2. The program prints i and increases it.
  3. if (i <= 5) goto start;: while i is 5 or less, jump back to start and repeat.
  4. When i becomes 6, the condition is false, the goto is skipped, and "Done" is printed.
  5. This works, but a for or while loop (next module) does the same job much more clearly.

How goto Works

  • When C reaches goto label;, it immediately continues from the line marked label:.
  • Nothing is checked by goto itself, which is why it is called unconditional. To jump only sometimes, put it inside an if.
  • goto cannot jump into another function.
  • Jumping forward over a variable's initialization, or into a block, can leave variables with garbage values, another reason to be careful.

Example 2: Ask Again on Wrong Input (Forward and Backward Jumps)

#include <stdio.h>

int main(void)
{
    int n;

ask:
    printf("Enter a positive number: ");
    scanf("%d", &n);

    if (n <= 0)
    {
        printf("Invalid! Try again.\n");
        goto ask;        /* backward jump */
    }

    printf("You entered %d\n", n);
    return 0;
}

Output

Enter a positive number: -5
Invalid! Try again.
Enter a positive number: 7
You entered 7

The same thing is usually written with a do-while loop, which you will learn soon.

Why goto is Avoided

ProblemExplanation
Hard to readYou have to search for labels to follow the flow
Hard to debugJumps make it unclear how the program reached a line
Spaghetti codeMany gotos tangle the logic
Not structuredLoops, if-else and functions express the same logic clearly

When is it acceptable? Experienced C programmers sometimes use one goto to jump out of deeply nested loops, or to jump to one clean-up section at the end of a function (closing files, freeing memory) when an error happens. The Linux kernel uses goto this way.

Important Points to Remember

  • goto is an unconditional jump to a label.
  • A label is a name followed by a colon: end:
  • The label must be in the same function.
  • goto makes code hard to read: spaghetti code.
  • Prefer loops, if-else, break and functions.

Common Mistakes Beginners Make

MistakeWhat happensFix
Label in another functionError: label used but not definedKeep goto and label in one function
Backward goto with no exit conditionInfinite loopPut the goto inside an if
label; instead of label:ErrorLabels end with a colon
Using goto instead of loopsMessy, hard-to-follow codeUse for / while

goto Statement Practice Questions

  1. What is the goto statement? Write its syntax.
  2. What is a label?
  3. Why is goto called an unconditional jump?
  4. Why should goto be avoided?
  5. Give one situation where goto may be acceptable.
Show answers
  1. An unconditional jump to a labelled statement in the same function. goto label; ... label: statement;
  2. A name followed by a colon that marks a place in the code.
  3. Because it jumps every time it is reached, without checking any condition itself.
  4. It makes code hard to read and debug (spaghetti code); structured statements do the job more clearly.
  5. Jumping out of deeply nested loops, or jumping to a common clean-up section on an error.

Coding Exercise

Using goto, print the multiplication table of 3 from 3 × 1 to 3 × 5. (Then think how a loop would make it simpler.)

Show solution
#include <stdio.h>

int main(void)
{
    int i = 1;

table:
    printf("3 x %d = %d\n", i, 3 * i);
    i++;
    if (i <= 5)
        goto table;

    return 0;
}
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15

Quick Quiz: goto Statement in C (MCQ)

  1. goto is a:

    A) Conditional jump   B) Unconditional jump   C) Loop   D) Function

  2. A label ends with:

    A) ;   B) :   C) .   D) ,

  3. A goto can jump to a label in:

    A) Any function   B) The same function only   C) Another file   D) main() only

  4. Code with many gotos is called:

    A) Clean code   B) Spaghetti code   C) Object code   D) Machine code

  5. Which is a better replacement for a backward goto?

    A) A loop   B) A comment   C) A macro   D) printf

Show answers
  1. B: unconditional jump.
  2. B: :
  3. B: the same function only.
  4. B: spaghetti code.
  5. A: a loop.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

gotoUnconditional jump to a label
Syntaxgoto label; ... label:
Label scopeSame function only
Jump typesForward and backward
Main problemSpaghetti code (hard to read/debug)
Acceptable useExit nested loops, error clean-up

Frequently asked questions

Why is goto considered bad practice?
It creates spaghetti code that is hard to read, test and debug.
Can goto jump to another function?
No. The label must be in the same function.
When is goto useful?
To break out of deeply nested loops or jump to a single clean-up section on error.
What is a label in C?
An identifier followed by a colon that marks a jump target.

Remember

  • goto = teleport; too many = spaghetti.
  • Prefer loops, break and functions.

Next Topic

Next lesson: Loops in C. Module 06 starts with loops: how to repeat code without writing it again and again.