Skip to main content

Infinite Loop in C

Infinite loop in C programming

An infinite loop in C programming is a loop that continues to execute indefinitely.

This can occur when the loop condition is always true or when the loop does not have a way to exit.

For Example:

#include <stdio.h>

int main() {
while(1) {
printf("This is an infinite loop\n");
}
return 0;
}

Explanation

This program uses a while loop with a condition of 1, which is always true. As a result, the loop will continue to execute indefinitely, printing "This is an infinite loop" over and over again.

To stop the program, you have to stop it manually by closing the terminal or using the keyboard interrupt (ctrl+c) to break the execution.

Uses of Infinite Loops in C Programming

Infinite loops can be used in various scenarios, some are listed below:

  • In a program that continuously waits for user input, an infinite loop can be used to repeatedly prompt the user for input until a valid response is received.

  • In a program that performs a certain task repeatedly, such as a game or animation, an infinite loop can be used to continuously update the game state or animation frame.

  • In a program that monitors a certain condition, such as the temperature of a device, an infinite loop can be used to repeatedly check the condition and take appropriate action if the condition is met.

  • In a program that continuously retrieves data from a sensor or other external source, an infinite loop can be used to repeatedly read the data and update the program's internal state.

  • In a program that runs on a microcontroller, infinite loop is used to continuously check and respond to input/output events and keep the microcontroller running.

Examples of Infinite Loops

Example 1

Below is a C Program, which uses Infinite Loop, and continuously waits for user input.

#include <stdio.h>

int main() {
int input;
while(1) {
printf("Enter a number (0 to exit): ");
scanf("%d", &input);
if(input == 0) {
break;
}
printf("You entered: %d\n", input);
}
printf("Exiting program...");
return 0;
}

Explanation:

  • The program starts by including the standard input/output library and declaring a variable "input" to store the user input.
  • An infinite loop is started using the while(1) statement.
  • Inside the loop, the program prompts the user to enter a number, using the printf function, and waits for the user to input a value using the scanf function which is stored in the "input" variable.
  • The program then checks if the input is equal to 0, if true, it uses the break statement to exit the infinite loop.
  • If the input is not 0, the program prints the message "You entered: %d\n" along with the input entered by the user.
  • After the infinite loop is broken, the program prints the message "Exiting program..." and finally returns 0.

Shell Output:

Enter a number (0 to exit): 2
You entered: 2
Enter a number (0 to exit): 3
You entered: 3
Enter a number (0 to exit): 0
Exiting program...
  • The program starts by prompting the user to enter a number, in this case, the user entered 2.
  • The program then displays the message "You entered: 2", as the input is not 0
  • Then the program again prompts the user to enter a number, and the user entered 3, the program then displays the message "You entered: 3"
  • The program again prompts the user to enter a number and the user entered 0, this time the program recognizes that the input is 0, thus it exits the infinite loop and displays the message "Exiting program..."

Best Practices for Using Infinite Loops

  • Always include a way to exit the infinite loop, such as by checking a user input or a specific condition.

  • Document the purpose and expected behavior of the infinite loop, including how it can be exited, to make it easier for other developers to understand and maintain the code.

  • Consider power consumption when using infinite loops in battery-powered devices.

  • Use appropriate timer functions or sleep functions to avoid high CPU usage

  • Use appropriate error handling and logging to detect and diagnose issues in infinite loops.

tip

You must maximize the benefits of infinite loops while minimizing the risks by following best practices such as providing an exit condition, testing, documenting, reducing resource usage, and handling errors.