Infinite Loops in C
You run your program and... it never stops. The screen keeps printing, or it just hangs. You have written an infinite loop: a loop whose condition never becomes false.
In this lesson you will learn the common causes of infinite loops, how to stop a running one, how to fix it, and when an infinite loop is actually written on purpose.
What You Will Learn
- What an infinite loop is.
- The 5 common causes of accidental infinite loops.
- How to stop a program stuck in a loop.
- How to write an intentional infinite loop with a safe exit.
- The floating-point comparison trap.
Infinite Loops in C Explained
Exam definition: An infinite loop is a loop whose condition never becomes false, so it repeats forever unless it is stopped from inside (with break or return) or from outside (by ending the program).
Memory trick: A loop is a walk to a finish line. An infinite loop happens when you either never take a step (no update), walk the wrong way (wrong update), or the finish line can't be reached (wrong condition).
How to stop a running infinite loop
- In a terminal: press Ctrl + C. This stops the program.
- In an IDE: click the Stop button.
- Then find the cause using the table below.
The 5 common causes
| # | Cause | Buggy code | Fix |
|---|---|---|---|
| 1 | Missing update | while (i <= 5) printf("%d", i); | Add i++ inside the loop |
| 2 | Update in the wrong direction | for (i = 1; i <= 5; i--) | i++ |
| 3 | Semicolon after while | while (i <= 5); { i++; } | Remove the ; |
| 4 | Condition always true | while (x = 1) or while (i != 10) with i += 3 | Use ==; use < instead of != |
| 5 | Comparing decimals with != | for (x = 0.0; x != 1.0; x += 0.1) | x < 1.0, or loop with an integer counter |
Syntax: Intentional Infinite Loops
while (1) { ... } /* most common */
for (;;) { ... } /* empty condition = always true */
do { ... } while (1);
Intentional infinite loops are used in menus, games and embedded devices (a washing machine program loops forever while it is switched on). They always have a planned exit, like break, or run until the power is turned off.
Why cause 4 and 5 are sneaky
In for (i = 0; i != 10; i += 3), i goes 0, 3, 6, 9, 12, 15... It jumps over 10, so i != 10 is always true. With decimals, 0.1 cannot be stored exactly, so adding it ten times gives 0.99999... or 1.00000001, never exactly 1.0. Using < instead of != fixes both.
Example 1: Intentional Infinite Loop with a Safe Exit
#include <stdio.h>
int main(void)
{
int count = 0;
while (1) /* would run forever... */
{
count++;
printf("Tick %d\n", count);
if (count == 3)
break; /* ...but break gives it an exit */
}
printf("Loop stopped safely.\n");
return 0;
}
Output
Tick 1 Tick 2 Tick 3 Loop stopped safely.
Code Explanation
while (1): 1 is always true, so the loop itself never stops.- Each round increases
countand prints it. - When
countreaches 3,breakends the loop. - This pattern is safe because the exit is clear and is always reached.
How to Find the Cause of an Infinite Loop
- Look at the loop condition. Which variable decides when it stops?
- Find where that variable changes inside the loop. If it never changes, that's the bug.
- Check the direction: does the change move it towards making the condition false?
- Check for a semicolon right after
while (...)orfor (...). - Add a
printfof the variable inside the loop to watch it change.
Example 2: Fixing the Floating-Point Loop
The buggy version for (x = 0.0; x != 1.0; x += 0.1) never ends. Here is the fixed version, using an integer counter:
#include <stdio.h>
int main(void)
{
for (int i = 0; i <= 10; i++)
{
double x = i / 10.0; /* 0.0, 0.1, ... 1.0 */
printf("%.1f ", x);
}
printf("\n");
int k;
for (k = 0; k < 10; k += 3) /* < instead of != */
;
printf("k stopped at %d\n", k);
return 0;
}
Output
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 k stopped at 12
Counting with an int is always exact, so the loop runs exactly 11 times. The second loop uses k < 10, so it stops at 12 instead of skipping past 10 forever.
Important Points to Remember
- An infinite loop's condition never becomes false.
- Stop a running program with Ctrl + C.
- Main causes: no update, wrong direction, semicolon, always-true condition, float compare.
- Prefer
</<=over!=in loop conditions. - Intentional loops:
while (1)orfor (;;)with a clear break.
Common Mistakes Beginners Make
| Mistake | Why it loops forever | Fix |
|---|---|---|
while (n > 0) { printf("%d", n); } | n never changes | Add n-- or n /= 10 |
for (i = 10; i > 0; i++) | i moves away from 0 | i-- |
while (choice = 1) | Assignment, always true | while (choice == 1) |
continue before i++ in while | The update is skipped | Update before continue |
Infinite Loop Practice Questions
- What is an infinite loop?
- Write three ways to create an intentional infinite loop.
- Why does
for (i = 0; i != 10; i += 3)never stop? - How do you stop a running program stuck in a loop?
- Why is
while (x != 1.0)dangerous with decimal numbers?
Show answers
- A loop whose condition never becomes false, so it repeats forever.
while (1),for (;;),do { } while (1);- i becomes 0, 3, 6, 9, 12... it skips over 10, so
i != 10is always true. - Press Ctrl + C in the terminal (or the Stop button in an IDE).
- Decimal values are stored approximately, so x may never be exactly 1.0.
Coding Exercise
This loop never ends. Find the bug and fix it so it prints 5 4 3 2 1:
int n = 5;
while (n > 0);
{
printf("%d ", n);
n--;
}
Show solution
The semicolon after while (n > 0) makes an empty loop that never changes n. Remove it:
#include <stdio.h>
int main(void)
{
int n = 5;
while (n > 0)
{
printf("%d ", n);
n--;
}
printf("\n");
return 0;
}
5 4 3 2 1
Quick Quiz: Infinite Loops in C (MCQ)
-
Which is an infinite loop?
A)
for (i = 0; i < 5; i++)B)while (1)C)while (0)D)for (i = 5; i > 0; i--) -
Which key combination stops a running program in the terminal?
A) Ctrl + S B) Ctrl + C C) Ctrl + Z only D) Alt + F4 only
-
for (;;)is:A) A syntax error B) An infinite loop C) Runs once D) Never runs
-
A loop condition should prefer:
A)
!=B)<or<=C)=D) Nothing -
Most common cause of an accidental infinite loop:
A) Too many printf B) Missing or wrong update C) Using int D) Comments
Show answers
- B:
while (1) - B: Ctrl + C.
- B: an infinite loop.
- B:
<or<= - B: missing or wrong update.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Infinite loop | Condition never becomes false |
|---|---|
| Intentional forms | while (1), for (;;), do {} while (1); |
| Stop running program | Ctrl + C |
| Main causes | No update, wrong direction, semicolon, always-true condition, float compare |
| Safer condition | < / <= instead of != |
| Used in | Menus, games, embedded systems |
Frequently asked questions
- What causes an infinite loop?
- A condition that never becomes false, usually a missing or wrong update.
- Is an infinite loop always a bug?
- No. Menus, servers and embedded programs use intentional infinite loops with a planned exit.
- How do you exit while (1)?
- Use
break(orreturn) when the exit condition is met. - Why avoid != with floats in loops?
- Floats are approximate, so the exact value may never be reached.
Remember
- Infinite loop = never step, wrong way, or unreachable finish line.
- Stuck? Ctrl + C, then check the update.
Next Topic
Next lesson: Pattern Programs in C. You will use nested loops to print star patterns, a top exam topic.