ITDeveloper : C Programming
C Programming
Step by Step Tutorials
![]() Share with a Friend |
Infinite Loop in C
An infinite loop is one that never ends and repeats itself eternally. An infinite loop is sometimes referred to as an indeterminate or endless loop. Although mistakes frequently result in the creation of an unending cycle, this does not indicate that they are not necessary or beneficial. Applications that require the application code to run endlessly until it stops, such as web servers, or those that receive user input and continuously produce output until the user leaves, operating system processes, games, and so forth, might utilise infinite loops.
Functions and Examples of Infinite Loop in C
The infinite loop in a program can be created in two ways:
- Unintentionally
- Intentionally
Unintentionally infinite loop gets create by bug in the code, by mistake or by specifying the condition which never becomes false. And intentionally infinite loop explicitly creates to achieve some requirement in an application. The loop structures we can use to create intentionally or explicitly infinite loop and run the code specified in a loop to repeatedly or infinite times. So we can use the following loops do create an infinite loop –
- for loop
- while loop
- do-while loop
- go to statement
- C macros
- For loop
Syntax:
for( ; ; )
{
// some code which run infinite times
}
In the above syntax three part of the for loop that is initialize, condition and increment/ decrement is not provided, which means no start value no end condition. So the loop run for infinite times.
Next, we write the c code to understand the infinite for loop working more clearly with the following example.
Code:
#include <stdio.h>
void main()
{
int i = 10;
for( ; ;)
{
printf("%d\n",i);
}
}
Output:
10
10
10
10
10
10
10
10
10
10
As in the above code the for loop is running for infinite times and printing the i value that is 10 infinitely.
Next we write the c code to show the kind of mistakes can lead to an infinite loop in for loop –
Code:
#include <stdio.h>
void main()
{
short int x;
for (x = 32765; x< 32768; x++)
{
printf("%d\t", x);
}
}
Output:
As above the loop is running infinite times because short int ranges is -32768 to 32767, so when i is the increment above to 32767 it becomes negative and hence the condition becomes always true.
- While Loop
Syntax:
while(1)
{
// some code which run infinite times
}
In the above syntax the condition pass is 1 (non zero integer specify true condition), which means the condition always true and the runs for infinite times.
Next we write the c code to create the infinite loop by using while loop with the following example.
Code:
#include <stdio.h>
void main()
{
int i = 10;
while(1)
{
printf("%d\t",i);
i++;
}
}
As in the above code while loop runs infinite times because the condition always becomes true and the i value is updated infinite times.
Next we write the c code to show the kind of mistakes can lead to an infinite loop in for loop –
Code:
#include <stdio.h>
void main()
{
int i = 10;
while(i<100)
{
printf("%d\t",i);
}
}
Output:
As in the above code the mistake is updating of I value is missing which leads to an infinite loop.
Other than this some more mistake which can lead to an infinite loop are:
- If Semicolon placed in the wrong position may lead to an infinite loop.
Example:
while(cond);
{
//code
}
- If logical conditions wrong by mistake, we used assignment operator (=) instead of a relational operator (= =) may lead to an infinite loop.
Example:
while(inp='y')
{
//code
}
- If loop condition mismatch may lead to an infinite loop.
Example:
for(int i=0;i>=0;i++)
{
//code
}
- Do-While Loop
Syntax:
do{
// some code which run infinite times
} while(1);
Next we write the c code to create the infinite loop by using do-while loop with the following example.
Code:
#include <stdio.h>
void main()
{
int i = 10;
do{
printf("%d\t",i);
i++;
} while(i);
}
- Goto Statement
Syntax:
label:// some code which run infinite timesgoto label;
Next we write the c code to create the infinite loop by using goto statement with the following example.
Code:
#include <stdio.h>
void checkEven(int num)
{
if (num%2 == 0)
goto even_no;
else
goto odd_no;
even_no:
printf("The number is even.\t");
goto even_no;
odd_no:
printf("The number is odd.\t");
goto odd_no;
}
void main()
{
int i = 10;
checkEven(i);
}
As in the above code the goto statement becomes the infinite loop.
- Macros
To create the infinite loop we can use macro which defines the infinite loop. Next we write the c code to create the infinite loop by using macro with the following example.
Code:
#include<stdio.h>
#define macro_name for( ; ; )
void main()
{
int i=10;
macro_name
{
printf("%d\t", i);
}
}
As in the above code the macro is defined whose value is for(;;). Later in a main function macro is used by its name, whenever the name of macro comes it gets replaced by its value.