Auto and Static keyword


auto

The auto keyword is used for the automatic declaration of the variable.
By default, all the variables are auto.
When the variable is not initialized, the value of the auto variable will be garbage value.
Syntax::  auto data_type Variable_name
Eg   #include<stdio.h>
         int main()
          {
                auto int i,j;
                printf(“%d %d”,i,j);
                return 0;
         }
Output::  1232 423 (Garbage values)
It is necessary to initialize the auto variable.

Program 1:
  #include<stdio.h>
   void fun();
   int main()
   {
       fun();
       fun();
       fun();
       return 0;
   }
   void fun()
   {
       auto int x=1;
       printf("%d\n",x);
       x=x+1;
   } 
 
Output:
1
1
1
 

Static

Whenever the variable value is initialized using a static keyword, that variable value cannot be reinitialized again in the entire program.
By default the value of static variable declared is zero. 
Example:
#include<stdio.h>
int main() 
{
  static int i;
  printf ("%d", i);
}
Output: 0
Example::

   Program 2:         
  #include<stdio.h>
   void fun();
   int main()
   {
       fun();
       fun();
       fun();
       return 0;
   }
   void fun()
   {
       static int x=1;
       printf("%d\n",x);
       x=x+1;
   } 
 
 Output: 
1
2
3

Observe program 1 and program 2.
The difference between these two programs is auto and static.
During the initialization of a variable, one has used auto and, the other has used static keyword.
There is a difference between both of the outputs.
In the first program, whenever the fun() is called the value of integer variable x is reinitialized.
Hence, the incremented value of x in the fun() gets initialized again, and there is no use of incrementation because the value of x always becomes 1.
In the second program, whenever the fun() is called the value of integer x remains the same and does not get initialized again.
Hence, the incremented value of x does not get initialized again to 1 after calling the function again.
auto and static are  are the Storage Classes in C
there are four storage classes in C
·     auto storage class
·     register storage class
·     static storage class
·     External storage class
To know in brief about Storage Classes in C
Follow this link.
 Also read:: Storage Classes in C Programming