Re: Is "scope" different from "visibility" ?
From: Jack Klein (jackklein_at_spamcop.net)
Date: 02/10/05
- Next message: Peter Nilsson: "Re: passing array to isdigit()"
- Previous message: TTroy: "Is "scope" different from "visibility" ?"
- In reply to: TTroy: "Is "scope" different from "visibility" ?"
- Next in thread: Mike Wahler: "Re: Is "scope" different from "visibility" ?"
- Reply: Mike Wahler: "Re: Is "scope" different from "visibility" ?"
- Reply: Ben Pfaff: "Re: Is "scope" different from "visibility" ?"
- Reply: Peter : "Re: Is "scope" different from "visibility" ?"
- Reply: Dave Thompson: "Re: Is "scope" different from "visibility" ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 09 Feb 2005 23:07:43 -0600
On 9 Feb 2005 20:04:04 -0800, "TTroy" <tinesan@gmail.com> wrote in
comp.lang.c:
> I have a few questions about "scope" and "visibility," which seem like
> two different things.
Scope is something defined by the C standard. "visibility" is not.
> To me "visibility" of the name of a function or object is the actual
> code that can use it in an actual program.
An object or function can only be referenced by name if it is in
scope.
> To me "scope" of the name of a function or object are the general rules
> for the areas of a program that can through a declaration, have
> "visibility."
> [By "to me," I'm stating that this is how I use the terms.]
But you are inventing a term not in the standard, and one that isn't
needed. An identifier must be in scope to be used. Period.
You are tangling up two actual C concepts, scope and linkage of
identifiers. But what you are calling visibility is nothing more or
less than scope.
Here is the text of paragraph 2 of section 6.2.1 Scopes of
identifiers, from 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 designated
by 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.)"
So an scope and visibility are the same thing.
> Ex:
>
> Externally defined variables have global scope <- Scope rule
No, there is no such thing as "global scope" in C. Externally defined
objects and functions have file scope. That scope extends from the
point of declaration (note that a definition is also a declaration) to
the end of the translation unit.
The other characteristic of objects defined at file scope is linkage.
They either have internal linkage, if the static keyword is part of
the definition, or external linkage by default if the static keyword
is not part of the definition.
But external linkage does not make an object or function usable by
name from another translation unit. That other translation unit must
have an external declaration of the object or function in scope when
it attempts to use it.
> The variable int i; in main.c has visibility in both main.c and test.c
> because there are declarations at the top of both files. <- describing
> visibility
If the declaration:
int i;
...appears at file scope in two different translation units that are
part of the same program, the behavior is undefined, because each of
these declarations is a 'tentative definition' that becomes an
external definition at the end of the translation unit.
> So it's as if, there are two steps to being able to access a function
> name or object name - first the code must fall in the scope of the said
> name, second the code must be in the visibility area of the said name
> (via a declaration).
No, what you are calling visibility is nothing more or less than
scope.
> Am I right? Scope can be determined when an object or function is
> defined, visibility can only be determined via declarations. If I'm
> not right, then which two terms differentiate the ACTUAL VISIBILTY AREA
> from the TOTAL POSSIBLE VISIBILITY AREA?
Scope is independent of linkage. Definitions or declarations inside a
block have scope that lasts to the end of that block, but block scope
declarations have no linkage. A file scope definition with the static
keyword also has no linkage.
> Also, say I have 2 source files main.c and second.c, and there is an
> external variable defined at the top of main.c.
>
> I only want to use this variable in 1 function in second.c, can I put
> the extern declaration inside the function's braces (will this limit
> others in second.c from accessing the variable?) ?
Yes, an external declaration within a block has scope that extends
from the declaration to the end of the block. So no other functions
in that source file will be able to access the object or function by
name.
> If the answer above is yes, then that is a better example of what I was
> saying above. The scope would be all code in both files, while the
> visibility would be all of main.c and just that function in second.c.
No, the scope will be all of the defining file after the definition,
except where it might be hidden by a declaration within a block
reusing the same identifier, if any.
In the second translation unit, the scope is begins at the external
declaration and end at the close of the block, which could be a top
level function block, or even a nested block inside the function.
The scope of an external declaration inside a function is not the
entire source file, it is just the remainder of the enclosing block
after the declaration.
> I haven't found enlightenment in any of the books or FAQs I've checked,
> so any help would be really appreciated.
The "visibility" you are talking about is exactly what the C language
defines as scope, no more and no less.
-- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
- Next message: Peter Nilsson: "Re: passing array to isdigit()"
- Previous message: TTroy: "Is "scope" different from "visibility" ?"
- In reply to: TTroy: "Is "scope" different from "visibility" ?"
- Next in thread: Mike Wahler: "Re: Is "scope" different from "visibility" ?"
- Reply: Mike Wahler: "Re: Is "scope" different from "visibility" ?"
- Reply: Ben Pfaff: "Re: Is "scope" different from "visibility" ?"
- Reply: Peter : "Re: Is "scope" different from "visibility" ?"
- Reply: Dave Thompson: "Re: Is "scope" different from "visibility" ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|