- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C infinite Loop
![]() Share with a Friend |
C Programming - C infinite Loop
Infinite Loop in C
An infinite loop is a loop that runs indefinitely because its termination condition is never met. In C, infinite loops can be created using while, for, or do-while loops. These loops are often used for programs that need to run continuously, such as server applications, or when waiting for user input indefinitely.
Syntax of Infinite Loop
- Using while Loop:
C
while (1) {
// Code to be executed
}
- The condition 1 (which is always true) causes the loop to run indefinitely.
- Since the condition never becomes false, the loop never exits.
- Using for Loop:
C
for (;;) {
// Code to be executed
}
- The for loop doesn't have any condition, initialization, or increment/decrement part, so it behaves like an infinite loop.
- This loop runs continuously because there's no condition to break out of it.
- Using do-while Loop:
C
do {
// Code to be executed
} while (1);
- Similar to the while loop, the condition 1 is always true, so the loop runs indefinitely.
- The key difference here is that the body of the loop is executed at least once before checking the condition.
Example 1: Infinite while Loop
C
#include <stdio.h>
int main() {
while (1) {
printf("This loop runs forever!\n");
}
return 0;
}
Explanation:
- The while(1) condition is always true, so the printf statement is executed continuously.
Output:
This loop runs forever!
This loop runs forever!
This loop runs forever!
...
This will continue until the program is forcibly terminated (e.g., by pressing Ctrl+C or closing the terminal).
Example 2: Infinite for Loop
C
#include <stdio.h>
int main() {
for (;;) {
printf("This is an infinite loop using for!\n");
}
return 0;
}
Explanation:
- The for(;;) loop has no conditions or increment/decrement, so it will loop forever.
Output:
This is an infinite loop using for!
This is an infinite loop using for!
This is an infinite loop using for!
...
Example 3: Infinite do-while Loop
C
#include <stdio.h>
int main() {
do {
printf("This is an infinite loop using do-while!\n");
} while (1);
return 0;
}
Explanation:
- The do-while(1) loop runs infinitely because the condition is always true.
- The body of the loop is executed at least once before checking the condition.
Output:
This is an infinite loop using do-while!
This is an infinite loop using do-while!
This is an infinite loop using do-while!
...
Common Use Cases for Infinite Loops:
- Server Programs: Server programs often use infinite loops to continually listen for requests or handle incoming data.
- Real-time Systems: Applications that monitor hardware or manage devices often need to run indefinitely to keep checking sensor values or respond to events.
- Waiting for User Input: Programs that wait for a user's input can use an infinite loop until the user decides to exit.
- Embedded Systems: In embedded systems, infinite loops are commonly used to keep the system running until it's powered off or reset.
Breaking an Infinite Loop:
An infinite loop can be terminated in several ways:
- Using break:
- You can manually exit an infinite loop by using the break statement inside the loop if a specific condition is met.
C
#include <stdio.h>
int main() {
while (1) {
int input;
printf("Enter 0 to exit: ");
scanf("%d", &input);
if (input == 0) {
break; // Exit the loop
}
}
return 0;
}
- Using Ctrl + C:
- In most terminal-based programs, you can interrupt an infinite loop with the Ctrl + C keyboard shortcut, which stops the program.
- Exit Program:
- You can use exit() to terminate the program completely.
C
#include <stdio.h>
#include <stdlib.h>
int main() {
while (1) {
// Code to execute
if (some_condition) {
exit(0); // Exit program
}
}
}
Summary:
- An infinite loop runs forever, often because the loop's condition never becomes false.
- Infinite loops can be created with while(1), for(;;), or do-while(1).
- They are commonly used in situations where the program needs to keep running continuously, such as in server applications, waiting for user input, or real-time systems.
- They can be terminated using break, exit(), or manually interrupting the program (Ctrl + C).
