A global variable's scope is in all the files, while a static global variable's scope is just the file where it is declared. Why so?
Where are global or static global variables stored in memory?
The difference between a static variable and a global variable lies in their scope. A global variable can be accessed from anywhere inside the program while a static variable only has a block scope.
A static global variable has internal linkage. A non-static global variable has external linkage.
A global static variable is one that can only be accessed in the file where it is created. This variable is said to have file scope. Constant Variables. In C, the preprocessor directive #define was used to create a variable with a constant value.
Global vs Static Variables They have external linkage, which means that in other source files, the same name refers to the same location in memory. Static global variables are private to the source file where they are defined and do not conflict with other variables in other source files which would have the same name.
There is some confusion, since static
in C can mean two different things. One is static storage duration, and the other is internal linkage. static
used as a keyword on file-scope will give the function or object it is used with internal linkage.
Internal linkage for a function or object means that if you declare another function in another "file" (this is not really called "file", but rather translation unit - TU), then that declaration will refer to a different function: The name declared in that unit will "link" to a different entity than the name declared in that other translation unit, which was "internal" to that TU. The same applies to objects.
Whether or not a file-scope variable is declared with static
, it will still have a static storage duration: That means it lives through the whole program, and dies when the program terminates. Another example of an object that has static storage duration is a string literal. Where objects that have static storage duration are stored isn't specified, but they are usually stored depending on whether they are initialized or not: Initialized file-scope variables are usually stored in a section called ".data", while non-initialized file-scope variables are usually stored in a section called ".bss". Remember that if the variable isn't initialized, it will be zero initialized at the start of the program: The ".bss" section is usually zero initialized by an implementation on program's startup.
I said "usually" everywhere, since where things are stored isn't specified. For example, some implementations could store string literals in a read-only section. And if you have a file-scope pointer and don't initialize it, the implementation initializes it to a null-pointer, which is not necessarily an object with all null bytes :)
They're both stored in the data segment; the difference is that the global has an externally-visible linker symbol, and the static global does not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With