Local Variables :
- Local variables are very short lived. Declared inside a function, they only exists in RAM as long as the function exists, the second their function ends, they disappear.
Global Variables :
- When we declare a variable outside of any function, its a global variable, this means that it is accessible in any function of the program. Unlike local variables, a global variable does not disappear at the end of a function, it persists until the program comes to an end. this is because typically, the operating system doesn’t store global variables in the stack or in the heap, but in a separate memory area. dedicated to globals ( data segment ).
Static Variables :
- Static variables have the property of preserving their value even after they are out of their scope, it preserves its previous value in its previous scope and is not initialized again in the new scope.
- it remains in memory while the program is running .
- they are allocated in the data segment.
- they are initialized as 0 if not initialized explicitly.
- they can only be initialized by constant literals.
- they shouldn’t be declared inside a structure (because c compiler requires the entire structure elements to be stored together).