Can anyone explain to me the reason behind the output of this program to be 0 0 0 0 0
?
Here we are using a static variable var
whose values will not change due to function calls. The values of var
will be 4, 3, 2, 1
during the recursive calls. When var
becomes zero the recursion terminates and control moves on to the printf
statement.
Why is the output not 1,2,3,4
?
main(){
static int var=5;
if(--var)
main();
printf(" %d ",var);
}
Again if you use if condition var--
then program output will be -1 -1 -1 -1 -1 -1
?
Every recursive function has two components: a base case and a recursive step. The base case is usually the smallest input and has an easily verifiable solution. This is also the mechanism that stops the function from calling itself forever.
A recursive definition of a function defines values of the function for some inputs in terms of the values of the same function for other (usually smaller) inputs. For example, the factorial function n! is defined by the rules. 0! = 1.
A recursive case has three components: divide the problem into one or more simpler or smaller parts of the problem, call the function (recursively) on each part, and. combine the solutions of the parts into a solution for the problem.
All functions - recursive or not - have one or more return . The return may or may not include a value. It is for you to decide when you write the function. All explicit written return statements must return the correct type.
In your recursion call printf()
executes when main()
returns. And because var
is a static variable
its value remain 0
(last value = 0 same for all function call)
Note if()
condition false when var
becomes 0
(last value, after main(); call you don't change var - notice diagram).
Hope following diagram will help you to understand (read comments):
main() <---------------+
{ |
static int var=5; | <----"Declared only one/first time with value 5"
if(--var) |
---- main(); ---------+ // called if var != 0
| // main called for var = 4, 3, 2, 1
|// recursion stooped
|// return with 0 value
|// now no operation applied on `var` so it remain 0
+--> printf(" %d ",var); // called when return ed
}
Remainder life of static function is till program terminates (so values not loss), and Scope is within function.
14.1.6 Static Variables
The scope of static automatic variables is identical to that of automatic variables, i.e. it is local to the block in which it is defined; however, the storage allocated becomes permanent for the duration of the program. Static variables may be initialized in their declarations; however, the initializers must be constant expressions, and initialization is done only once at compile time when memory is allocated for the static variable*.
Second question:
Again if you use
var--
then your output will be-1 -1 -1 -1 -1 -1
?
Suppose if your condition will be var--
then if()
condition fist checks true
or false
before decrement --
. (because in expression var--
, --
is postfix).
And because if()
breaks when var
== 0
then recursive call stops and function returns with decremented value from 0
to -1
. And because after return var
doesn't change hence output is -1
for all.
The values of var will be
4, 3, 2, 1
during the recursive calls. When var becomes zero the recursion terminates and control moves on to theprintf()
statement.Why is the output not1, 2, 3, 4
?
A static variable is a variable that has been allocated statically—whose lifetime or "extent" extends across the entire run of the program.
So the value of var changes every time and at last it becomes 0
and printf()
executes after the return of main and as the value of var is 0
,every printf()
statement will print 0
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With