S.C.
S.C.

Reputation: 231

What information does variable "scope type" provide that is not already described by variable "linkage type"?

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:

  1. Storage Duration - (automatic vs static)
  2. Scope - (block vs file)
  3. 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:

  1. If I know a variable has the feature no linkage, then I know that the scope type must be block
  2. 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:

Linkage Hierarchy

Upvotes: 2

Views: 191

Answers (1)

dbush
dbush

Reputation: 223972

While there is a relationship between scope and linkage, they represent two separate concepts.

Also, the C standard defines four different scopes for identifiers:

  • File
  • Block
  • Function
  • Function Prototype

This is specified in section 6.2.1p2 of the C standard:

For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope. Different entities designate dby the same identifier either have different scopes, or are in different name spaces. There are four kinds of scopes: function, file, block, and function prototype. (A function prototype is a declaration of a function that declares the types of its parameters.)

The only identifiers that have function scope are labels. A label can be references anyplace within the function were it is declared.

Identifiers with function prototype are function parameters that appear in a declaration of a function. For example:

void foo(int x);   //  x has function prototype scope

For identifiers with function scope or function prototype scope, they do not represent an object. Then in those cases the identifier has no linkage. Section 6.2.2p6 states:

The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier extern

So to answer your question, the linkage of an identifier doesn't imply its scope. An identifier with no linkage could have block scope, function scope, or function prototype scope.

Upvotes: 1

Related Questions