Infinite Loops in C: Types of Infinite Loops

Infinite Loops in C: Types of Infinite Loops

27 May 2024
Beginner
10.9K Views
7 min read
Learn with an interactive course and practical hands-on labs

Free C Foundation Course: Learn C In 21 Days

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.

Read More - Top 50 C Interview Questions and Answers

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

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 in the C Compiler, 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
... 

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

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

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

FAQs

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.

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.

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.

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.

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