Why does this program not run infinitely? [duplicate] - c

This question already has answers here:
Values obtained in case of a recursive function
(3 answers)
Closed 9 years ago.
Shouldn't this program run infinitely because main is being called every time? And why it's output is 0 0 0 0? I know it's a noob question but I am not able to get it. What --i do and what is the effect of declaring i as static?
int main()
{
static int i=5;
if(--i){
main();
printf("%d ",i);
}
}

The static int is not reinitialized each time. Thus, each time main() is called, i is one lower.
when (--i) is zero, the recursive loop terminates.
Thus, it starts off with i=5, and calls a new copy. This one has i=4, which again calls a new copy. This continues until i=0, at which point the function just terminates. Control flow is then returned up the call stack, and each copy of main prints i, which is now 0. 4 copies means 4 zeroes.

The reason it does not run forever is because at some point i becomes 0 and no longer calls main. The trick is static which references the same memory location and hence is decremented each time main is recursively called.

Related

why is the output contains 4 time 0's? [duplicate]

This question already has answers here:
Main calling main [duplicate]
(2 answers)
Why Output is 0000 and How? [duplicate]
(2 answers)
How does this program execute? [duplicate]
(5 answers)
understanding static int execution [duplicate]
(2 answers)
unexpected output in C (recursion)
(3 answers)
Closed 3 years ago.
why is the output contains 4 time 0's.The main call again and again until if condition become false and then it should be exit from if block.
#include <stdio.h>
int main()
{
static int i=5;
if(--i)
{
main();
printf("%d ",i);
}
}
Note the following.
the int i is static.
you are calling main reursively.
In the if condition you have a pre decrement of i
Every time you call main, the value of i will be the same as the previous call. So i will decrement each time. Since this is --i it will be 4 the first time and go to 0.
After the innermost main function returns(i==0), the printf of the main before that will be executed.
But i is static and has a value of 0. So you get 4 zeros printed for each of the main functions.

Execution of printf is ambiguous in a recursive main function [duplicate]

This question already has answers here:
unexpected output in C (recursion)
(3 answers)
Why Output is 0000 and How? [duplicate]
(2 answers)
Closed 5 years ago.
#include <stdio.h>
int main() {
static int i = 5;
if (--i) {
main();
printf("%d ", i);
}
}
The output is
0 0 0 0
I wonder how printf is executed in this program .
Well, i is static meaning that is shared among all calls to main.
You never print anything before i gets down to 0 and since i is shared to all calls at the point of the print all calls see the same value: 0
i=5 if(4) main()
i=4 if(4) main()
i=3 if(2) main()
....
i=0 now. All other calls will see this value. Resursions stops
print 0 cause i is static
print 0 cause i is static
print 0 cause i is static
To see what I think is what you expect print before the recursive call as follows:
int main() {
static int i = 5;
if (--i) {
printf("%d ", i);
main();
}
}
You need to put printf("%d ",i); above the inner call to main().
You're on thin ice here:
An implicit return 0; is inserted at the end of main if missing.
You can call main from itself in C; in C++ the behaviour is undefined.

C uninitialized int has a value of 1 instead of 0 [duplicate]

This question already has answers here:
Initializing variables in C
(10 answers)
Closed 6 years ago.
#include <stdio.h>
#include <string.h>
#include "prac.h"
#define MYNAME "Butter"
int main() {
int numberOfKids;
int weight;
int shirt;
printf("If I eat a Watermelon I will weigh %d lbs \n", weight + numberOfKids+ shirt );
return 0;
}
I compiled and ran the program and the result was 1; although I expected it to be 0. When I checked the value of each variable individually, the weight variable's value was 1. Can someone explain why that specific variables result was not 0? I am new to C and want to experiment with the basics to get a deeper understanding of the nuances of C. Any help would be appreciated.
Variables inside a function in C are not guaranteed to be set to anything by default. In memory, whatever was last stored there (which might not be flushed/erased to be 0) will be what the int is initialized to.
This is answered in Initializing variables in C
EDIT: As chux has stated below, local static variables are initialized to 0 if they aren't given an initial value. Also covered Is un-initialized integer always default to 0 in c?

Function call inside a loop in C [duplicate]

This question already has answers here:
C program - srand() [duplicate]
(6 answers)
Closed 7 years ago.
int random(){//function for random number generation
time_t t;
srand((unsigned) time(&t));
int rand = rand()%468;
return rand;
}
I want to put this function in a loop so it assigns a random number to a variable during each iteration of the loop, so i figured i would try this:
for(x=0;x<n;x++){
y=random();
printf("%d\n",y);
}
However this doesn't work as i expected, y just gets the number from the first iteration and repeats it, can someone explain how i can force the function to recall so it will set a new value to y?
You should not call srand() everytime before calling rand().
The way you are calling srand() always passes the same value as the seed, because the difference in seconds between each call is 0, since it's a lot smaller than 1s.
Since you always call srand() with the same seed and right before rand(), you will get always the same value from rand().
Just call srand() once at the start of the program, so in every run there will be different random numbers generated.

Why is the print statement encountered five times in the following program? [duplicate]

This question already has answers here:
Values obtained in case of a recursive function
(3 answers)
Closed 7 years ago.
According to the logic of recursion, the printf() at the end must be encountered only once. The main() after if() should send the control back to the beginning. Only after var becomes zero should we reach the printf(). Why do we encounter the printf() five times? I know that this has been asked before but this logic is not explained in there.
void main()
{
static int var=5;
if(--var){
main();
}
printf("%d\n", var);
}
The printf statement is not within the if. Each (recursive) call to main will ultimately call printf before terminating.
"the printf() at the end must be encountered only once" that's incorrect.
you enter main() five times, so you will exit it 5 times. Each invocation of main() enters a new copy of it, opening a new stack frame for it.
When the next invocation has printed its value and exited, the control returns to the previous invocation, at the point after the recursive call.
You should read the logic of recursion again i guess..
When the function is called from inside that function only, that presently executing function and all its parameters are pushed into the stack, and the newly called function starts executing. As this function calls gets over, the execution returns to the previous function (by popping it from the stack), and it will start executing from the point where it left..
Now, you can trace your program and check with his logic..

Resources