Difference between Local and Static variable

Started by thiruvasagamani, Sep 22, 2008, 11:09 AM

Previous topic - Next topic

thiruvasagamani

Difference between Local and Static variable :

Local variable :

    * It is declared by auto int a= 10
    * It must be declare inside the function
    * If it is not initialize garbage value is stored
    * It is initialized every time function is called that is repeated initialisation
    * It is lost when function terminates
    * It is created every time function is called

Static variable :

    * It is declared by static int a =10
    * It can be declared inside or outside the function
    * If declared inside a function, it is accessible only in that function. Scope is limited to that function and Lifetime is till program terminates.
    * If declared outside a function, the variable is global and is accessible by any function within that file. Scope is that file but Lifetime is till program terminates.
    * If it is not initialized 0 is stored by default
    * It is initialized once and only once
    * It is not lost , it doesnot mean that it can be accessed in different function
    * It is created only for the 1st function call same variable is used for subsequent call
Thiruvasakamani Karnan


Amit singh Rajpoot

this topic is very useful topic so thank you. and I have understood every point.    :acumen  :busted  :busted  :alien