What is the diffrenece between static variable and global variable?
Thanks in advance
Urmil
What is the diffrenece between static variable and global variable?
Thanks in advance
Urmil
A static variable doesn't get wipe out of memory when the function exits, but it can't be accessed everywhere in the program, unlike a global variable.
The main difference is the scope. A static variable defined inside a function, can only be used inside that function. The value of that variable will be preserved when the function is finished. Global variables can be used throughout the program. Although global variables may have their place, generally speaking, they should be avoided. Here's an example:
clu82Code:int global_int; /* global_int can be seen throughout the program */ int main(void) { func1(); ... } void func1() { static int local_int; /* local_int can only be seen inside func1 */ ... }
A variable declared within a function is a local variable. This variable only exists and can only be used when the function is called.
A global variable is a variable that can be used in any function in any file. A global variable is declared in one file and referred to by other files as extern.
A static is declared in one file and can only be used in that file. It's global to the functions in that file.