×

Infinite loop in C

Definition of infinite loop

The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes of this loop. It is also known as an endless loop or an indefinite loop.

There are a lot of applications where an infinite loop is helpful in running applications continuously. Those applications also produce endless outputs until the process is terminated manually. A few of them are the following:

  • Games: The user can play unlimited matches continuously, but if the user wants to stop the match, the h/.she has to exit the game manually. The game does not terminate on its own.
  • Operating Systems: As we usually observe, any operating system does not terminate its functioning on its own after the completion of the task until and unless the user manually shuts down the system.
  • Servers and website: Infinite loop is also used in servers and websites, which produce the continuous output as the user asks to carry out the process. This processing only gets dismissed when the user closes the website or when the administrator manually shuts the servers and websites down.

Now we will see how to initiate the infinite loop in various looping codes:

For loop

The statement for the infinite for loop is mentioned in the following block:

for(; ;)  // initiating "Infinite For Loop".
{  
    // main body of the "Infinite For Loop".  
}

In the above program line, no condition is mentioned so that it can produce infinite outcomes.

Now, it’s time to know precisely about this with the help of an example.

#include <stdio.h> // basic program line
int main()  
{  
    // initialising without any condition 
   for(;;)  
   {   
     printf("Hello, world!!!\t"); // main program line 
   }  
return 0;  
} 

Output:

Hello, world!!! Hello, world!!! Hello, world!!! Hello, world!!!
Hello, world!!! Hello, world!!! Hello, world!!! Hello, world!!!
Hello, world!!! Hello, world!!! Hello, world!!! Hello, world!!!
Hello, world!!! Hello, world!!! Hello, world!!! Hello, world!!!
Hello, world!!! Hello, world!!! Hello, world!!! Hello, world!!!
Hello, world!!! Hello, world!!! Hello, world!!! Hello, world!!! 
Hello, world!!! Hello, world!!! Hello, world!!! Hello, world!!!
Hello, world!!! Hello, world!!! Hello, world!!! Hello, world!!!
Hello, world!!! Hello, world!!! Hello, world!!! Hello, world!!!

While loop

// here, 1 represents the condition as true 
while(1) 
{  
   // main body of the "infinite while loop".  
}  

Example of Infinite while loop:

// basic program lines 
#include <stdio.h>  
int main()  
{  
  int a=1; // initialising the value of a as 1
  while(1)
  {  
      a++;   // increment or decrement statement 
      printf("now a is:%d \t",a);  
  }  
return 0;  
}  

Output:

188     now a is:125189        now a is:125190        now a is:125191    now a is:125192         now a is:125193        now a is:125194        now a is:125195     now a is:125196        now a is:125197        now a is:125198        now a is:125199     now a is:125200        now a is:125201        now a is:125202    now a is:125203         now a is:125204        now a is:125205        now a is:125206     now a is:125207        now a is:125208        now a is:125209        now a is:125210     now a is:125211        now a is:125212        now a is:125213    now a is:125214         now a is:125215        now a is:125216        now a is:125217     now a is :

Do-while loop

do  
{  
    // main body of the infinite do-while loop.  
}
while(1);  

Example of Infinite Do-while loop:

#include <stdio.h> // basic program line
int main()
{
do  
{  
   printf("Hello !!"); // main body of the infinite do while loop..  
}
while(1);  // 1 is considered as true and 0 as false
}

Output:

Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello!!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello !!Hello 

Related Topics

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 minutes read.

What is required in each C Program?

Each C program must require one function, i.e., main() function. It is because when we execute the C program, C compiler looks for the main() function, and from here only...

5 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.

Continue in C

C language: C language is a procedure oriented programming language. We can say that it is a platform dependent language. C language is introduced by Dennis Ritchie in the year 1970. We...

2 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

Break, continue and goto statement in C

Break statement Break statement is used to break the process of a loop (while, do while and for) and switch case. Syntax: break; Example 1 while(test Expression) { // codes if(condition for break){ break; } // codes } Example 2 For(int it, condition,...

1 minute read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.