Ayesha Gupta
Ayesha Gupta

Reputation: 145

Explain the output of a c program

#include<stdio.h>
main()
{
    static int i=1;
    printf("\nSoftware");                                                                   
    for(;i<5;i++)
        main();
    return 0;
}

The output comes out to be an infinite loop. Please explain.

Upvotes: 0

Views: 195

Answers (9)

user1803657
user1803657

Reputation:

Yeah definitely the output comes out to be infinite because in for(;i<5;i++) the increment of i occurs only at the last line of the for loop. So, here the value of i never incremented. for example:

for(i=0;i<5;i++)
{
int a,b; // just an example
a=10;
b=20;
printf("%d",i);
printf("%d %d",a,b);
// increment of i takes place here at the last line of for loop
}

Same as here also:

main()
{
    static int i=1;
    printf("\nSoftware");                                                                   
    for(;i<5;i++)
    {    
    main();
    // increment of i takes place here.But compiler never comes at this line .That's why i never incremented .
   }
   return 0;
}

Upvotes: 1

Sandeep Reddy Goli
Sandeep Reddy Goli

Reputation: 202

Calling main function in a loop eats up all your memory and causes stack overflow.

Upvotes: 1

Nargis
Nargis

Reputation: 4787

Its a kind of Recursive Call. Your main function is calling itself resulting into an infinite loop.

Upvotes: 0

Dmitriy Katkov
Dmitriy Katkov

Reputation: 46

i<5 call main()
i<5 call main()
i<5 call main()
...

If u want to call some function 5 times.

include

void myFunc()
{
   printf("\nSoftware"); 
}

main()
{
    static int i=1;                                                                
    for(;i<5;i++)
        myFunc();
    return 0;
}

Im used to c++ so it could be a some small mistake.

Upvotes: 0

nio
nio

Reputation: 5289

You are calling a main function from your main function. After you call new main function it will print some string and then again it will call main function.

Your i variable will not be incremented at all, because it is not incremented in the first iteration of for loop. After you call main, it will never return to previous main to next iteration of for loop to happen. So the loop will be infinite and i will be equal to 1. Even if you changed the for loop to any other loop, the loop will be still infinite.

I'm including your repaired code, just for the fun of it:

#include<stdio.h>

int main()
{
    static int i=0;
    if(i>=5) return 0;
    i++;
    printf("\nSoftware %i", i);
    main();
    return 0;
}

Upvotes: 3

Madhavan NR
Madhavan NR

Reputation: 188

This isn't seem to be any difficult or different.In this program for loop do not matter much, so whenever a main() is called inside the same it is definitely a infinite loop.With use of Conditional statements you can restrict the infinite loop. Also that static int do not matter at all.So obviously main get called infinite number of times until time out occurs. This is infact self explanatory

Upvotes: 1

Bart Friederichs
Bart Friederichs

Reputation: 33511

It is not an infinite loop.

Your are recursively calling main but never give an end to it. This results in a stack overflow.

i++ is never evaluated, because you never reach the end-of-scope of the for loop.

Upvotes: 1

Romesh
Romesh

Reputation: 2274

When one iteration of Loop finishes then value of i is incremented. But in your case before increment it is again calling the main() function. Hence your Code is in infinite loop.

Upvotes: 1

user2605046
user2605046

Reputation: 39

the code call main function, again and again

for(;i<5;i++)
main();

Upvotes: 0

Related Questions