Infinite loop in C Programming

infinite-loop-in-c-language-with-examples

Introduction:

We have discussed about the while loop, for loop, and do while loop in C language in our earlier articles, In today’s article we are going to learn about the Infinite loop in C language and different ways to create the Infinite loops.

Pre-Requisite:

We have briefly discussed the infinite loops in our Loops tutorials. It is recommended to go through the below loops tutorials

Infinite Loop in C programming:

Any loop can become an Infinite loop if the loop condition never becomes False. So the loop condition is always evaluated as True and the loop will run forever. This scenario is called Infinite Loop.

If you look at any of the loop syntaxes, We have a Loop Condition and loop control variable,

Here is an example program to print numbers from 1 to 5.

Program Output:

Here the loopControlVariable <= 5 is the Loop Condition and loopControlVariable is Loop Control variable

So each loop will have a Control variable ( loopControlVariable ), We need to make sure to increment or decrement the control variable loopControlVariable to meet our loop condition. In the above case, we incremented the loopControlVariable .

📢 If you forgot to increment or decrement the control variable, Then the loop condition won’t become False, Which means the loop condition always returns True. This causes the body of the loop to execute forever. This phenomenon is called Infinite Loop in programming.

How do Infinite Loops occur in C language?

We can have infinite loops in two ways

  • Un-Intentional Infinite loops
  • Intentional Infinite loops

let’s discuss about them in detail.

Un-Intentional Infinite loops in C Programming:

The Un-Intentional Infinite loops occur due to the bugs in our source code.

  • When we forgot to increment or decrement the loop control variable, etc.

Un-Intentional Infinite loops – Forgot to Decrement the Control Variable:

Here is an example program, where we want to print the numbers from 10 to 1 (Descending order)

C Program print numbers in Descending order:

Infinite loop Program Output:

Build and Run the program using any compiler.

Here we forgot to Decrement the counter (loop control variable) the value inside the while loop, So the value of the counter remained at the 10 forever.

We have the loop condition as while ( counter > 0 ), As the Counter value is not decreasing and staying at 10, So the loop condition will always return True and the loop runs forever. Which creates the Infinite loop.

If you run the above program, You will see the continuous printing of Counter value : 10 string on the console. Use the CTRL + C to kill the program (In Linux). Close the windows for PC users.

Avoiding Infinite loops:

To avoid the infinite loop, Make sure to change your loop control variables properly. You need to increment or decrement based on the loop condition.

For the above program, We can avoid the Infinite loop by Decrementing the counter value inside the while loop

Here is the working example, without an Infinite loop.

Program Output:

As you can see from the above output, Our program printed the numbers from 10 to 1 in descending order using the while loop. If you observe the program, We just added the counter--; to decrement the counter value.

So Initially the counter = 10 and at each iteration it will reduce by 1, So by the tenth iteration it reaches 1 and at the eleventh Iteration counter becomes 0 , The (counter > 0) loop condition will be evaluated as False and the while loop will terminate.

Intentional Infinite Loops in C language:

There will be many cases where you want to run your program forever.

For example, A Web Server can run forever to serve the requests from clients. In such cases, The web server needs to be online and running forever. In such cases, we use Infinite loops.

We can create the Infinite loop using all the loops and even using the goto statement.

  • while loop
  • for loop
  • do while loop
  • goto statement
  • Macro

let’s look at each one of them in detail.

Infinite loop in C using the while loop:

If you want to create the infinite loop intentionally, Then you can simply pass any True value to while condition like below, Which creates the infinite loop.

Syntax:

The above condition while(1) is evaluated as true because the value 1 is not a False value.

Program to Demonstrate Infinite while loop:

Program Output:

📢 Note: You can pass any True value to while condition, You are not restricted to using only 1

Here is another example, Where we are using the 100 as the loop condition, which is non-zero value so always returns True

Infinite while loop example 2:

Above code also creates the Infinite loop.

Infinite loops due to Mis-placed Semicolons:

If you add the semicolon at the end of the while condition then it will create an Infinite loop.

Syntax:

If you look at the above syntax, We have mistakenly added a Semicolon ( ; ) after the while condition , before the body of the while.

What does it do?

It actually creates a Dummy condition, The compiler will think, and the while loop is ended with the semicolon(;). and the body of the loop becomes another code block but not under the while loop.

So the body of the loop won’t be executed. We have loop control variable - i.e Counter, Which is incremented inside the body. But as the body is not executing, The counter value won’t increase and the LoopCondition won’t become false. This creates the Infinite loop.

Misplaced Semicolon (;) Program:

Program Output:

As you can see from the above output, Nothing will be printed.

Due to the semicolon at the end of the while loop ( while(counter <= 5);), The loop body won’t be executables. So the printf line won’t be executed and even the counter++ won’t be executed. So our loop condition never becomes False, Thus creating the Infinite loop.

📢 Don’t add the Semicolonafter the loop condition which creates a dummy loop, and the loop body won’t execute as part of the loop.

Avoiding Infinte loop:

To solve the infinite loop, Just remove the semicolon after the while condition like below.

Program Output:

Infinite loop in C using the for loop:

We can create the Infinite loops using the for loop using the following syntax

As you can see, We have not specified any condition for the loop, so the loop will never become False.

📢 All three statements (Init, condition, and update) of the for loop are optional.

Infinite for loop program:

Here is an example program

Program Output:

As you can see from the above output, The string Listening for requests will print forever. As we don’t have any condition and for loop never terminates.

Infinite for loop due to always True condition:

We can also create the Infinite for loop by specifying the Truevalue in the for loop condition field like this.

This program also runs forever and never returns. The output of this program is the same as the above program.

Misplaced semicolons in for loop:

The misplaced semicolons won’t create infinite loops in the case of the for loop. As for loop has the update statement as part of the loop options. So the counter will update.

📢 Misplaced semicolons won’t create infinite loops in the case of the for loop.

Have a look at the following program

Misplaced Semicolon Program:

Program Output:

Infinite loop in C using the do while loop:

We can create the Infinite loop using do while loop by simply passing the true value to while loop condition.

Here is the syntax

Here is an example program, which creates an Infinite loop.

Program – Infinite do while loop in C:

Program Output:

If you see the above code, We have specified the while(1) as our condition. So the loop will continue forever and we see continuous printing of numbers.

We have already looked at a Menu driven calculator program using the Infinite do while loop. Here is the link to it.

Infinite loop in C language using the goto statements:

We can create the Infinite loop using the go statements. Here is the syntax

Infinite loop in C Programming using the Macro:

We can create infinite loops using Marcos as well. Please look at the following example.

Program Output:

As you can see from the above program, We have created an Infinite loop using the Macro. As it is Macro, the Compiler will simply substitute the INFINITE_LOOP with while(1) during the Pre-Processing step.

After the pre-processing step above code will look like the below.

Pre-Processing is one of the steps in the compilation process of the C Program. To generate the source code after the preprocessing step, Use the following command.

If you open the pre-processor.i file, You will see the following code.

C-Code-after-pre-processing-step

Here the INFINTE_LOOP macro has been replaced by the while(1) expression. So it is essentially became the infinite loop using a while loop.

Conclusion:

We have discussed the Infinite loop in C language and Learnt about the Un-Intentional and Intentional Infinite loops. Finally, we looked at how we can create infinite loops using the different types of loops in C Programming Language.

C Programming Tutorials Index:

Loops Practise Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] Infinite loop in C Programming language with Examples […]

  2. […] the Infinite Loop and Check if the max is evenly divisible by num1 and num2 using ((max % num1 == 0) && (max […]

Leave a Reply