EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Infinite Loop in C 

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Infinite Loop in C 

Infinite Loop in C 

Introduction to Infinite Loop in C

A loop that repeats indefinitely and does not terminate is called an infinite loop. An infinite loop also called as endless loop or indefinite loop. An infinite loop is most of the time create by the mistake, but it does not mean that infinite loop is not require or not useful. Infinite loop can be use in an application where the application code is to be keep running for infinite until it is stopped example web server or where user input is to be accept and generate continuous output until user exits, operating system processes, games and so all.

Functions and Examples of Infinite Loop in C

The infinite loop in a program can be created in two ways:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  1. Unintentionally
  2. 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 –

  1. for loop
  2. while loop
  3. do-while loop
  4. go to statement
  5. C macros

1. 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);
}
}

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Output:

Infinite Loop in C Examples 1

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:

Infinite Loop in C Example 1

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.

2. 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++;
}
}

Output:

Infinite Loop in C Example 2

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:

Infinite Loop in C Example 2

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
}

3. 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);
}

Output:

Do-While Loop Examples 3

4. Goto Statement

Syntax:

label:
// some code which run infinite times
goto 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);
}

Output:

Goto Statement Examples 4

As in the above code the goto statement becomes the infinite loop.

5. 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);
}
}

Output:

Macros Examples 5

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.

Conclusion

An infinite loop is a loop that repeats indefinitely and does not terminate. A program can have infinite loop by intentionally or unintentionally as we have seen above. We have seen various ways to create an infinite loop and the solution to come out from infinite loop is use of break statement.

Recommended Articles

This is a guide to Infinite Loop in C. Here we discuss the Introduction to Nested Loop in C and its working along with the examples and code implementation. You can also go through our other suggested articles to learn more –

  1. Prime Numbers in C (Examples)
  2. How to Reverse Number in C?
  3. Introduction to Reverse String in C
  4. Prime Numbers in Java | Top 3 Examples

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More