473,394 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

The term "global variables" misleading?

j
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. So
would it be more correct to refer to such variables that are often
referred to with the term "global variable" as just variables whose
scope is file scope?
Nov 13 '05 #1
5 3464
j
Pieter Droogendijk <gi*@binky.homeunix.org> wrote in message news:<20030725221605.0f6a40f3.gi*@binky.homeunix.o rg>...
On 25 Jul 2003 09:49:32 -0700
ja****@bellsouth.net (j) wrote:

<snip>
Also, I don't see "global variable" used once in the standard. So
would it be more correct to refer to such variables that are often
referred to with the term "global variable" as just variables whose
scope is file scope?


They're called static or non-automatic. Reread your C book.


static? Standard certainly doesn't name them as such unless they are
explicitly declared with the static storage class specifier. I would
choose what the standard states over any book, anyday..

I was just looking for what people thought on the use of "global
variable" period, and if they thought it was a misleading term in
describing a variable whose scope is file scope and where the storage
is external..
Nov 13 '05 #2
j wrote:
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. So
would it be more correct to refer to such variables that are often
referred to with the term "global variable" as just variables whose
scope is file scope?


"global" == "of, relating to, or applying to a whole"

That leaves open the present context of "whole".

Sometimes "whole" is a file.
Sometimes "whole" is a program.

I acknowledge the ambiguity.
I disambiguate according to present discussion.
Nov 13 '05 #3
j wrote:

Pieter Droogendijk <gi*@binky.homeunix.org> wrote in message news:<20030725221605.0f6a40f3.gi*@binky.homeunix.o rg>...
On 25 Jul 2003 09:49:32 -0700
ja****@bellsouth.net (j) wrote:

<snip>
Also, I don't see "global variable" used once in the standard. So
would it be more correct to refer to such variables that are often
referred to with the term "global variable" as just variables whose
scope is file scope?


They're called static or non-automatic. Reread your C book.


static? Standard certainly doesn't name them as such unless they are
explicitly declared with the static storage class specifier. I would
choose what the standard states over any book, anyday..

I was just looking for what people thought on the use of "global
variable" period, and if they thought it was a misleading term in
describing a variable whose scope is file scope and where the storage
is external..


You're missing something j. The term static has two meanings in C. In
terms of storage, it is memory which is allocated and defined at compile
time as opposed to automatic, which is memory allocated and defined at
run time.
Objects defined outside of any function block have static storage class
because they are allocated their memory by the compiler and exist in
memory as the program loads and before it executes. If you want an
object of that same storage class within a function block, you declare
it 'static'.

The second meaning: Things defined outside of function blocks, including
functions have static storage class and external linkage. This external
linkage exposes the names of these objects to the linker so that
references to the objects in other modules can be resolved. For example
'printf' is a function in libc.a with external linkage. This allows you
to use 'printf()' in your programs without actually defining it. The
linker will 'point' your printf call into the library. Now this is the
slightly confusing part, if you want hide your otherwise externally
linked static storage objects from the linker, you declare them
'static'.

In foo.c for example...

int x; /* x has static storage and external linkage */
int foo(int i) {
return x + i;
}

In bar.c for example...

int x; /* x has static storage and external linkage */
int bar(int i) {
return x - i;
}

Now this is a conflict the linker can't resolve. If each x should be
local to its module, in each module declare it 'static int x;'. This
does not change the storage class (still static) but removes its
external linkage. x is now invisible to the outside world.

If we want one x used by both functions we might leave foo.c alone and
in bar.c (and any other module that might use it) declare it 'extern int
x;'. This refers x to the one defined with external linkage in foo.c.
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #4
Joe Wright <jo********@earthlink.net> wrote in message news:<3F**********@earthlink.net>...
Samuel Barber wrote:
By the way, this brings up a flaw in C. External variables and
(especially) functions should be 'static' by default.

Not a flaw. Objects defined outside any function, and functions
themselves have static storage class and external linkage. Declaring
them 'static' removes the external linkage without affecting storage
class.


Yes, that's the flaw. 'static' (internal linkage) ought to be the
default state, because the vast majority of globals and functions are
not meant to be public. So fastidious programmers put 'static' in
front of almost everything. And it's not just a matter of neatness; it
helps the compiler. For one thing, good compilers automatically inline
single-use functions - but only if they are declared 'static'.

The better way would have been to have "export" and "import" keywords
(or less clearly, "public" and "extern").

Sam
Nov 13 '05 #5
j wrote:
Anyone here feel that "global variables" is misleading for variables
whose scope is file scope?
I think the term "variable" is misleading. I use the term "file scope
objects with external linkage".
"global" seems to imply global visibility,
while this isn't true for variables whose scope is file scope.
That depends whether these so-called "variables" are qualified as static. If
so, then they are not even potentially globally visible (by name), although
there's nothing to stop their address being exported.

File scope objects that are not statically qualified are potentially visible
across the whole program.
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.
Yes. Just because you have your eyes shut, that doesn't make me invisible.
:-)

Also, I don't see "global variable" used once in the standard. So
would it be more correct to refer to such variables that are often
referred to with the term "global variable" as just variables whose
scope is file scope?


More "correct"? Well, I wouldn't care to venture an opinion on that. But to
avoid ambiguity, I try to avoid the terms "global" and "variable" whenever
I can.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #6

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

Similar topics

11
by: mrbog | last post by:
I have an array/hash that stores path information for my app. As in, what directory this is in, what directory that's in, what the name of the site is, what the products are called, etc. It's...
1
by: Chris Stromberger | last post by:
This doesn't seem like it should behave as it does without using "global d" in mod(). d = {} def mod(): d = 3 mod() print d
2
by: Eddy Ilg | last post by:
Hi, I am having a problem with an application I am writing: I have 2 scripts called 'conf' and 'build'. Both define a variable named 'root' and import a module named 'helper'. In the...
28
by: Alf P. Steinbach | last post by:
A few days ago I posted an "Hello, world!" tutorial, discussed in <url: http://groups.google.no/groups?threadm=41ba4c0a.76869078@news.individual.net>. As I wrote then: <quote> because there...
7
by: Lyn | last post by:
Hi and Season's Greetings to all. I have a question regarding the use of a qualifier word "Global". I cannot find any reference to this in Access help, nor in books or on the Internet. "Global"...
4
by: BB | last post by:
Hello all, I might be missing something here, but am trying to understand the difference between using application-level variables--i.e. Application("MyVar")--and global variables--i.e. public...
5
by: dave | last post by:
If I have a class that hold, for instance, user settings that should be accessible to the entire program logic, what is a good paradigm to use? In C++, I would have made it a global object,...
41
by: none | last post by:
Hello, IIRC, I once saw an explanation how Python doesn't have "variables" in the sense that, say, C does, and instead has bindings from names to objects. Does anyone have a link? Thanks, ...
4
ChrisWang
by: ChrisWang | last post by:
Hi, I am having trouble understanding the use of 'global' variables I want to use a global variable with the same name as a parameter of a function. But I don't know how to use them at the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.