Skip to content
StudyFame

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

#CauseBuggy codeFix
1Missing updatewhile (i <= 5) printf("%d", i);Add i++ inside the loop
2Update in the wrong directionfor (i = 1; i <= 5; i--)i++
3Semicolon after whilewhile (i <= 5); { i++; }Remove the ;
4Condition always truewhile (x = 1) or while (i != 10) with i += 3Use ==; use < instead of !=
5Comparing 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

  1. while (1): 1 is always true, so the loop itself never stops.
  2. Each round increases count and prints it.
  3. When count reaches 3, break ends the loop.
  4. This pattern is safe because the exit is clear and is always reached.

How to Find the Cause of an Infinite Loop

  1. Look at the loop condition. Which variable decides when it stops?
  2. Find where that variable changes inside the loop. If it never changes, that's the bug.
  3. Check the direction: does the change move it towards making the condition false?
  4. Check for a semicolon right after while (...) or for (...).
  5. Add a printf of 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) or for (;;) with a clear break.

Common Mistakes Beginners Make

MistakeWhy it loops foreverFix
while (n > 0) { printf("%d", n); }n never changesAdd n-- or n /= 10
for (i = 10; i > 0; i++)i moves away from 0i--
while (choice = 1)Assignment, always truewhile (choice == 1)
continue before i++ in whileThe update is skippedUpdate before continue

Infinite Loop Practice Questions

  1. What is an infinite loop?
  2. Write three ways to create an intentional infinite loop.
  3. Why does for (i = 0; i != 10; i += 3) never stop?
  4. How do you stop a running program stuck in a loop?
  5. Why is while (x != 1.0) dangerous with decimal numbers?
Show answers
  1. A loop whose condition never becomes false, so it repeats forever.
  2. while (1), for (;;), do { } while (1);
  3. i becomes 0, 3, 6, 9, 12... it skips over 10, so i != 10 is always true.
  4. Press Ctrl + C in the terminal (or the Stop button in an IDE).
  5. 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)

  1. 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--)

  2. Which key combination stops a running program in the terminal?

    A) Ctrl + S   B) Ctrl + C   C) Ctrl + Z only   D) Alt + F4 only

  3. for (;;) is:

    A) A syntax error   B) An infinite loop   C) Runs once   D) Never runs

  4. A loop condition should prefer:

    A) !=   B) < or <=   C) =   D) Nothing

  5. Most common cause of an accidental infinite loop:

    A) Too many printf   B) Missing or wrong update   C) Using int   D) Comments

Show answers
  1. B: while (1)
  2. B: Ctrl + C.
  3. B: an infinite loop.
  4. B: < or <=
  5. 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 loopCondition never becomes false
Intentional formswhile (1), for (;;), do {} while (1);
Stop running programCtrl + C
Main causesNo update, wrong direction, semicolon, always-true condition, float compare
Safer condition< / <= instead of !=
Used inMenus, 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 (or return) 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.