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?
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?
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.
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 ?
A global variable is intended to be accessible from any module - this practice it something considered poor practice, and should be used only if absolutely necessary.
File scoped static variables (what I assume you're talking about when you say "static global") can be accessed by the routines in a single compilation unit (generally a file) - the reason for this is to limit it's scope. When modifying code that uses the variable, there's a nice limit to where you need to look to see what other routines might be affected. It also reduces the opportunity for there to be a name clash.
When using global variables, if another set of modules happens to also use a global variable with the same name for a different purpose you'll have to modify one set to to use a different name. That problem doesn't exist for static variables.
Static globals cannot be accessed from other files while global variables can be accessed using the extern
keyword.
The C Standard doesn't specify where they are stored in memory, and it is be of no concern to the C programmer. The ability to access the variables is controlled by the compiler and the linker.
This is the second question you have asked on this topic area. One of the main reasons for using a relatively high-level programming language like C is that you don't have to worry about these issues.
We use static attribute to hide variable and function declarations inside modules, much as we would use public and private declarations in Java and C++. C source files play the role of modules. Any global variable or function declared with the static attribute is private to that module. Similarly, any global variable or function declared without the static attribute is public and can be accesses by any other module. Though it is a good practice to define as variables or functions with the 'global' keyword.
Both these variables are stored in .data or .bss section of a relocatable file.
Points to Remember to Crack Interviews Questions on Static and Global