Reputation: 308
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
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:
x3
in int x3;
) can denote various things:
MyStructure
in struct MyStructure
);goto Error; … Error: exit(EXIT_FAILURE);
).2{ … }
statement is a block, but there are others, not discussed here) is visible to the end of its enclosing block.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.
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
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