#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.
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;
}
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.
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.
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
Calling main function in a loop eats up all your memory and causes stack overflow.
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;
}
the code call main function, again and again
for(;i<5;i++)
main();
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.
Its a kind of Recursive Call. Your main function is calling itself resulting into an infinite loop.
Related
In the following code, would anything be returned?
#include <stdio.h>
int try_this (int in);
int main (void)
{
try_this (5);
}
int try_this (int in)
{
int i = 1;
for (i = 0; i < in; i = i + 2) {
return i;
}
return i;
}
Since there's a return in the for loop, would the code just return nothing since there's nothing after the function is called? Or would i be returned as a number, like 1(because of the declaration in try_this) or 6(because of the loop)?
Thank you!!:)
When the first return statement is encountered, the function will return. It's easy to see that the function will start the loop and while i=0 it will return i. So each time you call try_this you'll get 0
Also, return 0; from main...
When you enter the for loop for the first time, i is 0 and less than the 5. Then it execute the statement return i; in for loop, which will finish executing the try_this() funciton.
What you are basically asking is that if the function will return something even though there is no code after the function call.
The answer is yes, it will return. It doesn't matter if there is code later or not. The function will return (in this case 0) It's just that there's nobody to catch it.
The function try_this() always returns 0:
i is initially set to 1
then i is set to 0 in the initial clause of the for statement
if i < in, the loop body is executed and the value of i is returned, namely 0.
otherwise the loop is skipped and the next statement is return i; so 0 is returned too.
Regarding the main function: it ignores the return value of try_this(5) and reaches the end of its body without a return statement. This is bad style and would trigger undefined behavior for any other function, but since C99 the behavior is defined as a special case for the main function and the value 0 is returned to the startup code, which exits back to the system with a termination status of 0, which means successful execution.
if you want to store the value of 'I' i would recommend you to use "while loop"
int i = 0;
while(i < in)
{
i++;
}
Why is it that the variable 'a' changes along the first declaration of a=x (for x=-6) as the for-loop continues, and why doesn't it change for the decrementation of x?
In short, why is a=-4 at the end of the loop and not a=-8?
int main()
{
int x,a;
for (x=-6,a=x;x>-10;--x,a++)
x--;
printf ("%d %d",x,a);
return 0;
}
Due to the double decrementation of x, the loop runs twice, not four times. Hence a=-4 is correct. (In any case, a=-8 would never occur, as a is incremented from -6.)
Why does it give an error of segmentation fault(core dumped) and gives no output in 1st case ?Can anyone explain how program is program callls main recursively with parameters?
#include <stdio.h>
int main()
{
static int i = 2;
if (i<7)
{
main();
printf("%d ", i);
i++;
//main(10);
}
}
-
#include <stdio.h>
int main()
{
static int i = 5;
if (--i)
{
//main();
printf("%d ", i);
main(10);
}
}
You are calling main() first and then incrementing i
if (i<7)
{
main(); // <-- main called when i still 2
printf("%d ", i);
i++; // <-- statement never reached
//main(10);
}
Hence, while main() calls main() recursively, the statement i++ is never reached.
You should increment i before calling main()
if (i<7)
{
i++;
main();
printf("%d ", i);
//main(10);
}
1st case the i++ after the call of main(), that means the program has no chance to add the i, and into infinity loop, and stackoverflow! haha....
but the 2nd case, it's reduce the i before you call the main.
TL;DR StackOverflow (Pun or no pun, it's true both ways).
First: Some important information
This case has nothing to do with passing an argument to main() or not. Actually, as per the latest C standard, C11, for a hosted environment, the conforming signature of main() is int main(void) and following that, main(10); is wrong, altogether.
Now, coming to the actual problem here,
In the first case, change of i value happens after the call to main(), so in effect, the value of i never get changed because the control never reaches the statement which modifies i. Hence, it's an infinite loop, and because of the recursive call to main(), stack overflow happens.
In later case, i value gets updated before call to main(), so that value actually reflects. Thus, at some point (after 4 occurrences, actually), the if condition meets a FALSE condition and the recursive call ends and the stack unwinds.
First case:
i++ is positionned after the call to main. So i will always be equal to 2, as you never reach this part of the code. You are then calling your main function everytime, leading to an infinite recursion, and thus to the segmentation fault. The fix for this case would be to increase i before calling the main function.
if (i<7)
{
i++; // Increment before calling the main function, so i value is changed
main();
printf("%d ", i);
}
Do note that it will lead to something which looks like your second case, except that you will print several "7".
Seconde case:
In the second case, you are decreasing the value of i everytime you enter your if condition. When you finally can't enter your if condition anymore, it means i is equal to 0 (as if(0) is equivalent to if(false)). Everytime you will return to the previous called main function, i will still be equal to 0, explaining why you are displaying "0" everytime. If you want to print the different values, you can place your printf before the call to the main function for instance.
In the first case:
"i" is never incremented therefore you run in an infinite loop and get an overflow.
In the second case:
You call the recursion before displaying the value of "i", after four calls "i" equals to 0 and your recursion stack unwinds.
Modify your condition and perform outputs before the recursion call.
In the first case, i is never incremented, hence, the main keeps getting called and soon enough there is no more stack memory available to the program.
In the second case, the last updated value of i is intact throughout the lifetime of i. As i is static, program is referring to the same memory address on all the iterations. While printing i you see the last updated value, that is 0, as output.
Why is the code below resulting in an infinite loop?
#include <stdio.h>
void main()
{
int i;
for(i=0;i!=12;i++)
{
printf("%d\n",i);
i=12;
}
}
Because i never equals 12 when it's checked by the loop. You execute i++ after each loop iteration, so i always equals 13 when it's checked.
You can omit the i++ part entirely, or set i = 11; instead to accomplish the same thing. (Of course, since "the same thing" in this case is only ever wanting a single iteration of the loop, you don't really need a loop in the first place. But I assume this is just a contrived learning exercise.)
i++ is excecuted at the end of the loop so i will become 13
It happens because the for loop increments the variable before checking the loop condition.
Here's the code with the for loop rewritten as a while loop:
#include<stdio.h>
void main()
{
int i;
i=0;
while(i!=12)
{
printf("%d\n",i);
i=12;
i++;
}
}
And here's its output (the first few lines):
0
13
13
13
...
Each time through the loop, the code sets i to 12, and then immediately increments it to 13 before checking the condition and restarting the loop. The loop will only terminate when i==12, so it will run forever.
The following code gives output as 0 0 0 0 using Codeblocks.
int main()
{
static int i=5;
if(--i){
main();
printf("%d ",i);
}
}
I perfectly understands how the above code executes. However, when I removed 'static' from the code and used int i = 5, Ideone.com(online compiler) gave me runtime error and Codeblocks(using GCC) gave me nothing- even the terminal does not pop up.
I also tried placing the declaration part outside main i.e., static int i; and in main, I then gave i = 5;. Still, I am getting the above errors. I have no idea what is happening. Any help would be greatly appreciated.
PS: The program was found on a website and no explanation was given there.
If you remove the static that each call to main gets its own copy of i, initialized to 5, and so your recursion never terminates.
When you declare static int i outside main() and initializing it inside main() to value 5 has a problem when you execute. For the first time if(4) makes it call your inner main() which executes outer main() making i set back to 5 leading it to infinite loop, hence no output you see because your if never fails and only one possibility it has is if(4).
int main() // 1st main call
{
static int i=5;
if(--i){ // if(4), if(3), if(2), if(1)-> all four if's are true
// if(0) fails
main(); // 2nd main, 3rd main, 4th main, 5th main -> corresponding to
// above successful if's.
// When if(0), recursion ends, return
printf("%d ",i); // Now i is `0` and prints 4 zero's
}
}
If you remove static, you get an infinite recursion before you output anything. The recursion leads to a stack overflow, and the program crashes.
The static means you operate always on the same variable, so you can count from 5 -> 0. if you remove it, you initialize it at every call with 5, and if(4) is always true.
With static it is only initialized once.
when using static printf will show 4,3,2,1 and the recursion will end.
when not using static, theoretically (if the program would run) the printf will show 4,4,4,4..
and the recursion will never end.
There is no problem in your code.
CASE 1:
if you write, it will give you output 0000 cause very time the you are decrementing the value of i. once it become 0. if condition will be false and it will print 0 four times.
int main()
{
static int i=5;
if(--i){
main();
printf("%d ",i);
}
}
CASE 2:
but if you remove the static, the in every recursive call you are creating a new variable i which will take 2 byte each time and consume memory(in stack). Once the stack full, it prompt runtime error and program will be crashed. The online compiler of http://ideone.com
handle the runtime error in backend so that your program will not crashed.that why you are getting runtime error. and expected answer.
CASE 3:
and when you declare your variable outside like
static int i;
int main()
{
i=5;
if(--i){
main();
printf("%d ",i);
}
}
In this case it will again cross the memory limit. here each time you are assigning the 5 to i and main() will take space in stack, again program crash.