In the book "C Programming: A Modern Approach", Chapter 18 takes on some of the more technical features of declarations. On page 459, the author states that a variable in a C program has three properties:
- Storage Duration - (automatic vs static)
- Scope - (block vs file)
- Linkage - (external vs internal vs none)
I've poked around on several different websites and several differ S.O. posts, but I am still having some difficulties understanding the differences between scope and linkages.
One of the major conceptual difficulties I have is determining the unique information that is provided by specifying the scope of a variable that is not already given by the linkage type. There seem to be some nuances that I am completely missing.
Consider the following examples of declarations for variables:
Case 1:
void foo(void)
{
static int j; /* block scope + no linkage */
}
Case 2:
int i; /* file scope + external linkage */
int main(void)
{
.
.
.
}
Case 3:
static int i; /* file scope + internal linkage */
int main(void)
{
.
.
.
}
If you look at the 3 cases, you see that block scope pairs with no linkage and file scope pairs with either internal or external linkage.
Therefore, I see no information that is added by specifying the scope. That is to say:
- If I know a variable has the feature no linkage, then I know that the scope type must be block
- If I know a variable has the feature internal linkage or external linkage, then I know that the scope type must be file.
I am unfamiliar with code that utilizes block scope with internal linkage. I am unfamiliar with code that utilizes block scope with external linkage. Finally, I am unfamiliar with code that uses file scope with no linkage.
So it seems like the type of linkage provides all the relevant information needed...so what exactly is the point of scope?
Thanks!
Edit:
Said differently, these terms are not independent (there are certain combinations that simply don't exist, at least that's how it seems).
Here's a picture that I think captures the idea correctly:
