Local Global and Static variables in C

Local Variables
The variables which are declared inside the function, compound statement (or block) are called Local variables.
You can use the same variable names in a different function and they will not conflict with each other. 

Global Variables
The variables declared outside any function are called global variables. They are not limited to any function. Any function can access and modify global variables. Global variables are automatically initialized to 0 at the time of declaration. Global variables are generally written before main() function.
Unlike local variables, global variables are not destroyed as soon as the function ends. They are available to any function until the program is executing.

Static variables
A Static variable is able to retain its value between different function calls. The static variable is only initialized once, if it is not initialized, then it is automatically initialized to 0.

No comments:

Post a Comment