#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Loops

Infinite Loops in C: Types of Infinite Loops

What is an Infinite Loop?

An infinite loop is a loop whose terminating condition never becomes false, so it keeps executing forever unless explicitly broken out of.

Examples

// Using for
for (;;) {
    printf("Running\n");
}

// Using while
while (1) {
    printf("Running\n");
}

// Using do-while
do {
    printf("Running\n");
} while (1);

When Are They Useful?

  • Server or event-listening programs that must run continuously.
  • Menu-driven programs that loop until the user chooses to exit, using break.

Accidental infinite loops are a common bug — always ensure there's a valid exit condition or break statement.

How an Exit Condition Actually Ends a Loop

A loop keeps running while its condition is true. Writing while (1) or for (;;) makes the condition permanently true, so nothing inside the loop can end it by the condition alone — something must actively break out. The usual fix is to change a value that the condition reads, or to use break. If neither happens, the loop runs forever and the program hangs.

int count = 0;
while (1) {
    printf("%d\n", count);
    count++;
    if (count == 5) break;   // the exit the loop needs
}

Breaking Out Safely With break and Flags

Intentional infinite loops power menu programs and event listeners. The pattern is to loop forever on purpose and leave only on a clear signal — a menu choice, an end-of-input, or an error:

int choice;
while (1) {
    printf("1=run  0=exit: ");
    if (scanf("%d", &choice) != 1) break;  // bad input ends it too
    if (choice == 0) break;                // user chose to quit
    printf("running...\n");
}

Checking the return value of scanf matters: if input fails and you do not handle it, the loop can spin forever reading the same bad data.

Spotting an Accidental Infinite Loop

  • The counter never reaches the condition. Incrementing by 2 while testing i == 5 from an odd start skips the target forever — test with < or <= instead of ==.
  • The update is inside an if that rarely runs, so most iterations change nothing.
  • A floating-point counter compared for exact equality, which rounding may never satisfy.
  • A missing break in a loop that was meant to exit on a condition.

When a program freezes and burns CPU, suspect a loop whose exit is never reached. Print the controlling variable inside the loop to see whether it is moving toward the condition at all.

Two C-specific traps deserve a mention. A stray semicolon right after the header, as in while (i < 10);, gives the loop an empty body, so the real block runs once afterward and i never advances. And using assignment = where you meant comparison ==, as in while (x = 5), sets x to 5 every time and tests a value that is always true. Both compile cleanly, so the compiler will not warn you unless you turn warnings up.

Takeaway: an infinite loop is only a bug when it is unintentional. Deliberate ones are fine as long as there is a reliable break; accidental ones nearly always come down to an update that never pushes the condition toward false.

Ready to master real-world C Programming development?

Learn C Programming hands-on with mentor-led, live sessions.

Explore Course