Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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?

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.

Static variables are those variables whose life time remains equal to the life time of the program.Static variables get are initialized once. The value of the variable will change after every system call . If you had not declared the variable as static there would have been an infinite recursion resulting in an segmentation fault .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top