Browse Tutorials
Infinite Loops in C: Types of Infinite Loops

Infinite Loops in C: Types of Infinite Loops

05 Dec 2023
Beginner
4.05K Views
9 min read
Learn via Video Course & by Doing Hands-on Labs

C Programming For Beginners Free Course

Infinite Loops in C Programming: An Overview

The concept of infinite loops has value in the world of C programming, but it also has significant drawbacks. For jobs ranging from developing games to real-time systems, these loops, which run indefinitely without an exit condition, are incredibly powerful. However, using them incorrectly can cause program crashes and unresponsiveness. This article delves into the topic of endless loops in C, examining its types, uses, and best practices for maximizing their benefits while minimizing the risks they bring to programmers.

What is an Infinite Loop in C?

An infinite loop is a sequence of instructions that is executed endlessly without termination. This occurs when the loop condition remains true, or when the programmer forgets to apply a terminating statement within the loop body.

Although infinite loops might seem inefficient and troublesome, they can sometimes serve useful purposes. C's infinite loops are useful for real-time systems but they must be used carefully. Inadvertent errors can cause programs to freeze, use up resources, and degrade performance.

When to Use an Infinite Loop in C?

Infinite loops are generally used when the users want to execute a section of code continuously until a specific condition is met.

Some common scenarios where an infinite loop can be used include:

  • While writing a server program it needs to continuously listen for incoming client requests and respond to them. Here, an infinite loop can be used to keep the server running and process requests until explicitly stopped.
  • When creating a game or simulation program, an infinite loop can be used to repeatedly update the game state and render the graphics until the game exits.
  • While implementing any real-time application that, requires a continuous stream of data or events.

How to Create an Infinite loop?

With the use of different loop structures, we can produce an infinite loop. The loop structures that we will use to define the endless loop are as follows:

  1. For loop in C
  2. While loop in C
  3. Do-while loop in C
  4. Goto statement in C
  5. C macros

For loop

An infinite for loop gets created if the for loop condition remains true, or if the programmer forgets to apply the test condition/terminating statement within the for loop statement.

Syntax

for(; ;) 
{ 
 // body of the for loop. 
}

In the above syntax, there is no condition hence, this loop will execute infinite times.

Example of an infinite for loop in C


#include <stdio.h>

int main() {
 for (;;) {
 printf("Hello World\n");
 }

 return 0;
}

In the above code, we have run the for loop infinite times, so "Hello World" will be displayed infinitely.

Output

Hello World
Hello World
Hello World
Hello World
Hello World
... 

While loop in C

If the condition of a loop is always true, the loop becomes an infinite one.

Syntax

while(true) 
{ 
 // body of the loop.. 
} 

Example of an infinite while loop in C


#include <stdio.h>

int main() {
 char str[] = "Infinite while loop";
 int i = 0;

 while (1) {
 i++;
 printf("i is: %d\n", i);
 }

 // The return statement may never be reached in an infinite loop.
 return 0;
}
 

Here, the value of i will be printed n number of times due to the absence of a terminating condition.

Output

i is: 1
i is: 2
i is: 3
i is: 4
i is: 5
... 

Do...while loop

It gets created when the testCondition remains true.

Syntax

do 
{ 
 // body of the loop.. 
}while(1);

Example of an infinite do...while loop in C


#include <stdio.h>

int main() {
 do {
 printf("This is an infinite do-while loop.\n");
 } while (1);

 // The return statement may never be reached in an infinite loop.
 return 0;
}
 

Output

This is an infinite do-while loop.
This is an infinite do-while loop.
This is an infinite do-while loop. 
...

Goto statements in C

  • It is a control flow mechanism that enables programmers to move the execution of a program to a particular labeled statement inside the code.
  • Allows one to skip over the customary sequential flow of the code and go to a specific location within it.
  • The best practice is to avoid the requirement for goto statements whenever possible by using structured programming elements like loops and conditionals.

Syntax

infinite_loop; 
// body statements. 
goto infinite_loop; 

C Macros

Macros are preprocessor instructions that give programmers strong resources.

  • They improve code modularity and cut down on duplication by enabling programmers to define reusable code parts.
  • C macros can significantly increase productivity and code clarity when used properly, but care must be taken to avoid complexity and unwanted consequences.

Syntax

#define MACRO_NAME replacement_value

FAQs

1. What is an infinite loop in C?

When a series of instructions in a C program repeats continuously without an exit condition, it is known as an infinite loop and the program will continue to run indefinitely until manually stopped or terminated.

2. What are the types of infinite loops?

In C, "while(true)" loops and "for(;;)" loops are the two main types of endless loops. These loops execute continuously because there are no exit conditions.

3. How do you create an infinite loop in C?

You can use a "while(true)" loop or a "for(;;)" loop without any exit conditions in C to produce an infinite loop. Infinite loop example:

while (true) {
 // Code that repeats indefinitely
}

4. How do you stop an infinite loop in C?

In C, you usually have to manually stop the program from running in order to break an infinite loop. The Ctrl+C keyboard shortcut or other platform-specific techniques can be used to do this.

5. What is an infinite loop, and how do you avoid it?

An infinite loop occurs when the execution of the code never ends. Make sure your loops have the appropriate exit conditions, such as "break" statements or logical conditions that turn into false when the required condition is satisfied, to prevent it.

6. Can you provide an example of an infinite loop?

Here is a "while(true)" loop in C that uses an infinite loop.

#include <stdio.h>int main() {
 while (true) {
 printf("This is an infinite loop.\n");
 }
 return 0;}

The message will be constantly printed by this program, which won't stop unless it is manually stopped.

Summary

In this C++ tutorial, we saw various ways to make an infinite loop in C. To fully utilize the potential of C's infinite loops, proper coding, debugging, and exit conditions are essential. For more understanding, you can consider our C Certification program.

Share Article
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Accept cookies & close this