- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- 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 - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
C - Infinite Loop
In C language, an infinite loop (or, an endless loop) is a never-ending looping construct that executes a set of statements forever without terminating the loop. It has a true condition that enables a program to run continuously.
Flowchart of an Infinite Loop
If the flow of the program is unconditionally directed to any previous step, an infinite loop is created, as shown in the following flowchart −
An infinite loop is very rarely created intentionally. In case of embedded headless systems and server applications, the application runs in an infinite loop to listen to the client requests. In other circumstances, infinite loops are mostly created due to inadvertent programming errors.
How to Create an Infinite Loop in C?
To create an infinite loop, you need to use one of the loop constructs (while, do while, or for) with a non-zero value as a test condition. Generally, 1 is used as the test condition, you can use any non-zero value. A non-zero value is considered as true.
Example
#include <stdio.h> int main() { while (1) { printf("Hello World"); } return 0; }
Hello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello ...
Types of Infinite Loops in C
In C language, infinite while, infinite do while, and infinite for are the three infinite loops. These loops execute the code statement continuously. Let us understand the implementation of infinite loops using all loop constructs.
Infinite While Loop
The while keyword is used to form a counted loop. The loop is scheduled to repeat till the value of some variable successively increments to a predefined value. However, if the programmer forgets to put the increment statement within the loop body, the test condition doesn’t arrive at all, hence it becomes endless or infinite.
Example 1
Take a look at the following example −
#include <stdio.h> // infinite while loop int main(){ int i = 0; while (i <= 10){ // i++; printf("i: %d\n", i); } return 0; }
Output
Since the increment statement is commented out here, the value of "i" continues to remain "0", hence the output shows "i: 0" continuously until you forcibly stop the execution.
i: 0 i: 0 i: 0 ... ...
Example 2
The parenthesis of while keyword has a Boolean expression that initially evaluates to True, and is eventually expected to become False. Note that any non-zero number is treated as True in C. Hence, the following while loop is an infinite loop:
#include <stdio.h> // infinite while loop int main(){ while(1){ printf("Hello World \n"); } return 0; }
Output
It will keep printing "Hello World" endlessly.
Hello World Hello World Hello World ... ...
The syntax of while loop is as follows −
while (condition){ . . . . . . }
Note that the there is no semicolon symbol in front of while, indicating that the following code block (within the curly brackets) is the body of the loop. If we place a semicolon, the compiler treats this as a loop without body, and hence the while condition is never met.
Example 3
In the following code, an increment statement is put inside the loop block, but because of the semicolon in front of while, the loop becomes infinite.
#include <stdio.h> // infinite while loop int main(){ int i = 0; while(i < 10);{ i++; printf("Hello World \n"); } return 0; }
Output
When the program is run, it won't print the message "Hello World". There is no output because the while loop becomes an infinite loop with no body.
Infinite For Loop
The for loop in C is used for performing iteration of the code block for each value of a variable from its initial value to the final value, incrementing it on each iteration.
Take a look at its syntax −
for (initial val; final val; increment){ . . . . . . }
Example 1
Note that all the three clauses of the for statement are optional. Hence, if the middle clause that specifies the final value to be tested is omitted, the loop turns infinite.
#include <stdio.h> // infinite for loop int main(){ int i; for(i=1; ; i++){ i++; printf("Hello World \n"); } return 0; }
Output
The program keeps printing Hello World endlessly until you stop it forcibly, because it has no effect of incrementing "i" on each turn.
Hello World Hello World Hello World ... ...
You can also construct a for loop for decrementing the values of a looping variable. In that case, the initial value should be greater than the final test value, and the third clause in for must be a decrement statement (using the "--" operator).
If the initial value is less than the final value and the third statement is decrement, the loop becomes infinite. The loop still becomes infinite if the initial value is larger but you mistakenly used an increment statement.
Hence, both the for loops result in an infinite loop.
Example 2
Take a look at the following example −
#include <stdio.h> int main(){ // infinite for loop for(int i = 10; i >= 1; i++){ i++; printf("Hello World \n"); } }
Output
The program will print a series of "Hello World" in an infinite loop −
Hello World Hello World Hello World ... ...
Example 3
Take a look at the following example −
#include <stdio.h> int main(){ // infinite for loop for(int i = 1; i <= 10 ; i--){ i++; printf("Hello World \n"); } }
Output
The program keeps printing "Hello World" in a loop −
Hello World Hello World Hello World ... ...
Example 4
If all the three statements in the parenthesis are blank, the loop obviously is an infinite loop, as there is no condition to test.
#include <stdio.h> int main(){ int i; // infinite for loop for ( ; ; ){ i++; printf("Hello World \n"); } }
Output
The program prints "Hello World" in an endless loop −
Hello World Hello World Hello World ... ...
Infinite Do While Loop
An infinite loop can also be implemented using the do-while loop construct. You have to use 1 as the test condition with the while.
Example
The following example demonstrates an infinite loop using do while:
#include <stdio.h> int main() { do { printf("Hello World\n"); } while (1); return 0; }
Hello World Hello World Hello World Hello World ... ...
How to Break an Infinite Loop in C?
There may be certain situations in programming where you need to start with an unconditional while or for statement, but then you need to provide a way to terminate the loop by placing a conditional break statement.
Example
In the following program, there is no test statement in for, but we used a break statement to make it a finite loop.
#include <stdio.h> int main(){ // infinite while loop for(int i = 1; ; ){ i++; printf("Hello World \n"); if(i == 5) break; } return 0; }
Output
The program prints "Hello World" till the counter variable reaches 5.
Hello World Hello World Hello World Hello World
How to Stop an Infinite Loop Forcefully in C?
When the program enters an infinite loop, it doesn’t stop on its own. It has to be forcibly stopped. This is done by pressing "Crtrl + C" or "Ctrl + Break" or any other key combination depending on the operating system.
Example
The following C program enters in an infinite loop −
#include <stdio.h> int main(){ // local variable definition int a = 0; // do loop execution LOOP: a++; printf("a: %d\n", a); goto LOOP; return 0; }
Output
When executed, the above program prints incrementing values of "a" from 1 onwards, but it doesn’t stop. It will have to be forcibly stopped by pressing "Ctrl + Break" keys.
a: 1 a: 2 ... ... a: 10 a: 11 ... ...
Infinite loops are mostly unintentionally created as a result of programming bug. Even if the looping keyword doesn’t specify the termination condition, the loop has to be terminated with the break keyword.