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
start:is a label. It does nothing by itself; it just marks the line.- The program prints
iand increases it. if (i <= 5) goto start;: while i is 5 or less, jump back tostartand repeat.- When i becomes 6, the condition is false, the goto is skipped, and "Done" is printed.
- This works, but a
fororwhileloop (next module) does the same job much more clearly.
How goto Works
- When C reaches
goto label;, it immediately continues from the line markedlabel:. - 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
| Problem | Explanation |
|---|---|
| Hard to read | You have to search for labels to follow the flow |
| Hard to debug | Jumps make it unclear how the program reached a line |
| Spaghetti code | Many gotos tangle the logic |
| Not structured | Loops, 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
| Mistake | What happens | Fix |
|---|---|---|
| Label in another function | Error: label used but not defined | Keep goto and label in one function |
| Backward goto with no exit condition | Infinite loop | Put the goto inside an if |
label; instead of label: | Error | Labels end with a colon |
| Using goto instead of loops | Messy, hard-to-follow code | Use for / while |
goto Statement Practice Questions
- What is the goto statement? Write its syntax.
- What is a label?
- Why is goto called an unconditional jump?
- Why should goto be avoided?
- Give one situation where goto may be acceptable.
Show answers
- An unconditional jump to a labelled statement in the same function.
goto label; ... label: statement; - A name followed by a colon that marks a place in the code.
- Because it jumps every time it is reached, without checking any condition itself.
- It makes code hard to read and debug (spaghetti code); structured statements do the job more clearly.
- 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)
-
goto is a:
A) Conditional jump B) Unconditional jump C) Loop D) Function
-
A label ends with:
A)
;B):C).D), -
A goto can jump to a label in:
A) Any function B) The same function only C) Another file D) main() only
-
Code with many gotos is called:
A) Clean code B) Spaghetti code C) Object code D) Machine code
-
Which is a better replacement for a backward goto?
A) A loop B) A comment C) A macro D) printf
Show answers
- B: unconditional jump.
- B:
: - B: the same function only.
- B: spaghetti code.
- A: a loop.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| goto | Unconditional jump to a label |
|---|---|
| Syntax | goto label; ... label: |
| Label scope | Same function only |
| Jump types | Forward and backward |
| Main problem | Spaghetti code (hard to read/debug) |
| Acceptable use | Exit 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.