Abhishek Jaiswal
Abhishek Jaiswal

Reputation: 308

What is lexical scope in c language?

I was reading K&R book and came across a paragraph which is out of understanding for me.

there are two kinds of scope to consider: first, the lexical scope of an identifier which is the region of the program text within which the identifier's characteristics are understood; and second, the scope associated with objects and functions with external linkage, which determines the connections between identifiers in separately compiled translation units.

Can someone elaborate the paragraph as well as the lexical scope please??

Upvotes: 1

Views: 719

Answers (2)

Eric Postpischil
Eric Postpischil

Reputation: 222900

The C standard uses different and more precise language. What Kernighan and Ritchen call “lexical scope” is simply called “scope” in the C standard. C 2018 defines scope in clause 6.2.1 and linkage in 6.2.2. Briefly, scope is where in the source code an identifier is “visible” during compilation of one source file, and linkage is a method of connecting the same identifier between different compilations. In more detail, but still summarizing:

  • An identifier (a name in source code, like the x3 in int x3;) can denote various things:
    • an object1;
    • a function;
    • a tag (MyStructure in struct MyStructure);
    • a member of a structure, union, or enumeration; a typedef name; or
    • a label (for goto statements, as in goto Error; … Error: exit(EXIT_FAILURE);).2
  • Identifiers have various kinds of scope, which I will not discuss fully:
    • Function scope: A label is visible throughout the function in which it appears.
    • File scope: An identifier declared outside any function is visible from its declaration to the end of the file (except where hidden3).
    • Block scope: An identifier declared inside a block (each { … } statement is a block, but there are others, not discussed here) is visible to the end of its enclosing block.
    • Function prototype scope: An identifier within the parameter declarations of a function declaration that is not a definition is visible to the end of the function declarator.
  • Each time you compile a source file, identifiers are made known where they are declared (or, for labels, where they appear on a labeled statement). Identifiers in other source files are not known or visible; they are not in scope.
  • Even if the same identifier appears in two different source files, the compiler does not know they refer to the same thing.
  • After compiling source files to object files, the object files are linked to make a program. During this process, some identifiers are resolved to refer to the same thing.
  • Linkage is a property describing how different instances of the same identifier can be made to refer to the same thing.
  • An identifier with no linkage is never made to refer to the same thing as another instance of the same identifier; each declaration of an identifier with no linkage refers to a unique thing.
  • An identifier with internal linkage is resolved within the source file being compiled; all references are resolved internal to the single compilation.
  • An identifier with external linkage is resolved to refer to the same thing as the same identifier used in other source files with external linkage.

We may think of scope as a unitary thing: The compiler recognizes an identifier throughout its entire scope and processes it internally. In contrast, with external linkage, the compiler puts information about the identifier and the thing it refers to in the object file, and the linker reads information from different object files and unites them. Wherever identifiers with external linkage are used in object files, the linker changes the code somewhat so that the different uses of the identifiers refer to the same thing.

Footnotes

1 Students may think of the x created by int x = 3; as a variable. The C standard treats this more technically: There is the identifier x, which is text in the source code, and there is the memory where the value is stored. The C standard calls this memory an object, and treating a variable as two parts, an identifier that denotes an object, lets us discuss and analyze the concepts better.

2 An identifier can also denote a macro or macro parameter, but I will not cover those in this answer.

3 The same identifier can be declared in a nested scope, as in int x = 3; … { float x = 4.5; … } }. The scope of the outer identifier includes the inner region, but the outer identifier is hidden in the inner scope.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

Strictly speaking there is no such a normative term as "lexical scope" in the C Standard.

It seems the author of the book means scopes of identifiers (lexical scope) and their linkages.

From the C Standard (6.2.2 Linkages of identifiers)

1 An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage. There are three kinds of linkage: external, internal, and none.

Upvotes: 1

Related Questions