473,748 Members | 7,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is "scope" different from "visibility " ?

I have a few questions about "scope" and "visibility ," which seem like
two different things.

To me "visibility " of the name of a function or object is the actual
code that can use it in an actual program.
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.]

Ex:

Externally defined variables have global scope <- Scope rule

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
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).

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?

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?) ?

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.

I haven't found enlightenment in any of the books or FAQs I've checked,
so any help would be really appreciated.

Nov 14 '05 #1
8 3377
On 9 Feb 2005 20:04:04 -0800, "TTroy" <ti*****@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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
"Jack Klein" <ja*******@spam cop.net> wrote in message
news:dq******** *************** *********@4ax.c om...

[snip]
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.
static int i; /* internal linkage? */
[snip]
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.


static int i; /* no linkage? */
-Mike
Nov 14 '05 #3
Jack Klein <ja*******@spam cop.net> writes:
On 9 Feb 2005 20:04:04 -0800, "TTroy" <ti*****@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.


The standard does not specifically define "visibility " but it
does use the term "visible" a fair amount, e.g.:

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.
....
Within the inner scope, the identifier designates the entity
declared in the inner scope; the entity declared in the
outer scope is hidden (and not visible) within the inner
scope.

Unless explicitly stated otherwise, where this International
Standard uses the term ``identifier'' to refer to some
entity (as opposed to the syntactic construct), it refers to
the entity in the relevant name space whose declaration is
visible at the point the identifier occurs.
....
If more than one declaration of a particular identifier is
visible at any point in a translation unit, the syntactic
context disambiguates uses that refer to different entities.
--
"I should killfile you where you stand, worthless human." --Kaz
Nov 14 '05 #4
On Thu, 10 Feb 2005 17:00:53 GMT, "Mike Wahler"
<mk******@mkwah ler.net> wrote in comp.lang.c:
"Jack Klein" <ja*******@spam cop.net> wrote in message
news:dq******** *************** *********@4ax.c om...

[snip]
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.
static int i; /* internal linkage? */


Yes, absolutely.

========
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.

2 In the set of translation units and libraries that constitutes an
entire program, each declaration of a particular identifier with
external linkage denotes the same object or function. Within one
translation unit, each declaration of an identifier with internal
linkage denotes the same object or function. Each declaration of an
identifier with no linkage denotes a unique entity.

3 If the declaration of a file scope identifier for an object or a
function contains the storage class specifier static, the identifier
has internal linkage.
========

Note the meaning of the term in the middle of the paragraph 2, and how
it is produced in paragraph 3.

Consider this translation unit:

int a; /* file scope external linkage */
int a; /* ditto */

extern int b; /* file scope external linkage */
extern int b; /* ditto */

static int c; /* file scope internal linkage */
static int c; /* ditto */

void func(void)
{
int d; /* block scope, no linkage */
int d; /* ditto */

static int e; /* block scope, no linkage */
static int e; /* ditto */
}

And here's the diagnostics produced by one compiler (MSVC++ 6.0):

Compiling...
clc.c
C:\Program Files\Microsoft Visual Studio\MyProjec ts\clc\clc.c(13 ) :
error C2086: 'd' : redefinition
C:\Program Files\Microsoft Visual Studio\MyProjec ts\clc\clc.c(16 ) :
error C2086: 'e' : redefinition
Error executing cl.exe.

clc.obj - 2 error(s), 0 warning(s)
[snip]
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.

Got me there, tragic loss between brain and fingers.
static int i; /* no linkage? */
-Mike


--
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #5

"Jack Klein" <ja*******@spam cop.net> wrote in message
news:u1******** *************** *********@4ax.c om...
On Thu, 10 Feb 2005 17:00:53 GMT, "Mike Wahler"
<mk******@mkwah ler.net> wrote in comp.lang.c:
"Jack Klein" <ja*******@spam cop.net> wrote in message
news:dq******** *************** *********@4ax.c om...

[snip]
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.


static int i; /* internal linkage? */


Yes, absolutely.


That's what I thought.

[snip ISO quote and examples]
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.
Got me there, tragic loss between brain and fingers.


I thought so, but wanted to make sure I wasn't missing
something. (Whenever I'm in doubt about something in
C, you're one of the folks here whose explanations I
give the most weight. But alas you're human too. :-) )
static int i; /* no linkage? */


Perhaps I should have phrased my reply differently
(e.g. 'was that a typo?').

I really didn't mean for you to go to all that
trouble posting a quote and examples. Sorry
about that, and thanks.

-Mike
Nov 14 '05 #6
On Fri, 11 Feb 2005 05:54:34 GMT, "Mike Wahler"
<mk******@mkwah ler.net> wrote in comp.lang.c:

"Jack Klein" <ja*******@spam cop.net> wrote in message
news:u1******** *************** *********@4ax.c om...
On Thu, 10 Feb 2005 17:00:53 GMT, "Mike Wahler"
<mk******@mkwah ler.net> wrote in comp.lang.c:
"Jack Klein" <ja*******@spam cop.net> wrote in message
news:dq******** *************** *********@4ax.c om...

[snip]

> 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.

static int i; /* internal linkage? */


Yes, absolutely.


That's what I thought.

[snip ISO quote and examples]
> 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.


Got me there, tragic loss between brain and fingers.


I thought so, but wanted to make sure I wasn't missing
something. (Whenever I'm in doubt about something in
C, you're one of the folks here whose explanations I
give the most weight. But alas you're human too. :-) )


Depends on who you ask! :(
static int i; /* no linkage? */


Perhaps I should have phrased my reply differently
(e.g. 'was that a typo?').

I really didn't mean for you to go to all that
trouble posting a quote and examples. Sorry
about that, and thanks.

-Mike


That's OK, I'm well wound up tonight. I even corrected an error by
Victor Bazarov over on the brand x group, with three quotes from the
brand x standard, and that's almost as rare as catching a Lawrence
Kirby error here, now that Lawrence is back.

--
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7
Groovy hepcat Jack Klein was jivin' on Wed, 09 Feb 2005 23:07:43 -0600
in comp.lang.c.
Re: Is "scope" different from "visibility " ?'s a cool scene! Dig it!
On 9 Feb 2005 20:04:04 -0800, "TTroy" <ti*****@gmail. com> wrote in
comp.lang.c:

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.


Uh..., internal linkage, I think, Jack.

-------------------------------------------------------------------
6.2.2 Linkages of identifiers
....
3 If the declaration of a file scope identifier for an object or a
function contains the storageclass specifier static, the identifier
has internal linkage.20)
-------------------------------------------------------------------

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 14 '05 #8
On Wed, 09 Feb 2005 23:07:43 -0600, Jack Klein <ja*******@spam cop.net>
wrote:
<snip>
An object or function can only be referenced by name if it is in
scope.
Right. Or perhaps more precisely, if the/a declaration is in scope.

<snip 6.2.1p2>
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.
True but incomplete; all 'external' (meaning top-level) definitions
_and declarations_ have file scope. That meaning is different from
'external linkage' (which you correctly describe) which can be
confusing. (Ada calls the equivalent 'library level', which is
confusing in a different way.) All definitions are semantically also
declarations, but formally function definitions are not syntactically
declarations while object definitions are, which makes it clumsier to
state rules that are both clear and correct, unfortunately.
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.
Did you mean (only) object here, or object-or-function=entity ? It's
true either way, but needs to be slightly modified: if the function or
file-scope object is declared with 'static' it has internal linkage,
whether or not that declaration is the definition. In most cases it
is, but we have composite cases like:

static int a; /* (forward) declaration and tentative definition */
static void f (void); /* forward declaration */
...
int a = 10; /* definition, overrides t.d., still static=internal */
void f (void) { blah blah blah } /* defined, still internal */
/* even if you actually put 'extern' on the definition,
they still have internal linkage! */
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.
Right. In particular, a nondefining declaration also with external
linkage, which in effect means simply 'extern whatever'.

<snip> 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.
No, file-scope with static has internal linkage -- as you already
said. Maybe you meant to say that block-scope declarations with static
have no linkage -- they are always private to that block, unlike
file-scope ones -- while block-scope declarations with extern do have
external linkage like file-scope ones (modulo as above). The "normal"
case for block-scope declarations, automatic (and register) objects,
have no linkage.

(And declarations of types and tags, and enums, never have linkage.
They are compiletime only and don't exist at runtime -- the standard
isn't permitted to actually say so because it would be considered
constraining implementations , but that's the reason.)

<snip> 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.

They won't be able to access it using that declaration. There could be
_another_ declaration for the entity in or visible to other functions.

<snip rest>

- David.Thompson1 at worldnet.att.ne t
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1333
by: user | last post by:
Hi. Sorry for the probable dumbness of my question, but the behaviour I'm observing seems a blatant defect in the C++ Standard or in the implementation I'm using (several different versions of GCC, all behaving exactly the same, GNU binutils, GNU/Linux on PowerPC). Let's say I have two compilation units, a.cc and b.cc; each of them need to perform some initialization (or finalization, for what it matters) which I want to be transparent,...
5
3492
by: j | last post by:
Anyone here feel that "global variables" is misleading for variables whose scope is file scope? "global" seems to imply global visibility, while this isn't true for variables whose scope is file scope. If you have a variable whose scope is file scope in another translation unit, you have to provide a local declaration to access that variable from the other translation unit. Also, I don't see "global variable" used once in the standard....
39
3189
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the function changes the state of the object, it ought to be a member of that object." Reference Accelerated C++, A. Koenig, page 159.
14
6022
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
3
12344
by: VRSki | last post by:
Hello, Is there any way to use "using" for aliasing in the global scope? The example below works fine in the context of a given file, but in the different file MyByte alias is unknown. Is there any way to extend its visibility to multiple files without redefining it? using MyByte=System.Byte;
30
3838
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at the end of the app for example: class test { public: int x;
4
8416
hemantbasva
by: hemantbasva | last post by:
We have designed an aspx page having five ajax tab in it. the data of first four are designed on page whereas for the fifth tab it renders a user control named MYDOMAIN. in the tab container's even onactivetabindexchanged we have called a method loadtabpanel() which is defined in javascript in same page.the problem is it sometime give the message load tab panel undefined. this error does not come regularly. it comes in usercontrol rendering . i...
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.