Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to eliminate this global variable, silent?

17 views
Skip to first unread message

istil...@gmail.com

unread,
Apr 12, 2008, 12:31:41 AM4/12/08
to
When I control if I print messages, I usually use a global variable
"int silent". When I set "-silent" flag in my command line
parameters, I set silent = 1 in my main.c.


I have many functions that may print some messages.

foo(...)
{
if (!silent)
printf("Msg1\n");
}


foo2(...)
{
if (!silent)
printf("Msg2\n");
}


and so on...


Is the above bad coding practice? How to eliminate the variable
"silent" but achieve the same effect?

Barry Schwarz

unread,
Apr 12, 2008, 1:00:12 AM4/12/08
to

If you are using for debugging, it is fine.


Remove del for email

Ian Collins

unread,
Apr 12, 2008, 1:16:39 AM4/12/08
to
istil...@gmail.com wrote:
> When I control if I print messages, I usually use a global variable
> "int silent". When I set "-silent" flag in my command line
> parameters, I set silent = 1 in my main.c.
>
>
> I have many functions that may print some messages.
>
> foo(...)
> {
> if (!silent)
> printf("Msg1\n");
> }
>
>
> foo2(...)
> {
> if (!silent)
> printf("Msg2\n");
> }
>
>
> and so on...
>
>
> Is the above bad coding practice?

No, it's quite common for debugging.

> How to eliminate the variable "silent" but achieve the same effect?

Create a wrapper for printf and link with either a debug or production
version. If you simply want to remove the global, make silent static
and expose it through a getSilent() function.

--
Ian Collins.

Pradeep

unread,
Apr 12, 2008, 7:51:01 AM4/12/08
to istillsh...@gmail.com
Hi,
Here is modification for your program.
--------------
foo()
{
#if !SILENT
printf("Msg1\n");
#endif

}

foo2()
{
#if !SILENT
printf("Msg2\n");
#endif
}
main()
{
foo();
foo2();
}
--------------
command: gcc -D SILENT foo.c
$add '-D SILENT' to compiler option
Your program should work.
i have tested successfully in gcc-4.0 compiler.

-- Pradeep
On Apr 12, 10:16 am, Ian Collins <ian-n...@hotmail.com> wrote:

istil...@gmail.com

unread,
Apr 12, 2008, 9:07:37 AM4/12/08
to
On Apr 12, 1:16 am, Ian Collins <ian-n...@hotmail.com> wrote:

> Create a wrapper for printf and link with either a debug or production
> version.

Suppose the wrapper name is called info. How to implement it? I
tried to write it,as shown below. But failed to do it.


#ifdef DEBUG
#define info() /* how ? */
#else
#define info() /* how? */
#endif /* DEBUG */

Eric Sosman

unread,
Apr 12, 2008, 10:46:37 AM4/12/08
to

Are you the same "istillshine" who in another thread
is lecturing us all about the use of `const'? If so, I
hope you'll pardon me for declining to accept advice from
someone who can't even read the FAQ. Question 10.26, to
be specific.

--
Eric Sosman
eso...@ieee-dot-org.invalid

istil...@gmail.com

unread,
Apr 12, 2008, 10:51:40 AM4/12/08
to
On Apr 12, 10:46 am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:

> Are you the same "istillshine" who in another thread
> is lecturing us all about the use of `const'?

I think "lecturing" was not my attitude when I posted that thread. If
it appeared to be so, I am sorry. I posted it because I felt confused
on that point. I wanted to see what other people think.


> If so, I
> hope you'll pardon me for declining to accept advice from
> someone who can't even read the FAQ. Question 10.26, to
> be specific.

Oh it's there. I did not notice it. Thank you for pointing it out to
me.

Ben Bacarisse

unread,
Apr 12, 2008, 1:27:58 PM4/12/08
to
Pradeep <pradeep....@gmail.com> writes:

> Here is modification for your program.
> --------------
> foo()
> {
> #if !SILENT
> printf("Msg1\n");
> #endif
> }

<snip more>

That is a modification that reduces the usability of the program. The
original had a run-time settable "silent" option.

For many simple programs I would not mind a few "global"[1] variables
that control the overall behaviour of the system. However, since
flags tend to breed, I'd consider:

struct program_options {
unsigned silent: 1;
/* there will be more, I can feel it */
};
struct program_options_s program_options;

Now, having done that[2], why put the other "global" data in there as
well (and maybe change its name). Then, when your program becomes a
component in a bigger system, you are already set. After all,
"global" variables should have nice long names, and

program_options.silent

is not much harder to use than

program_options_silent

[1] Can we not permit the phrase "global variable" into c.l.c to mean
an object defined at file scope with external linkage? It would save
a lot of typing.

[2] The down side, is that one can't use the 'silent' member in many
option parsing systems since you can't take its address. If that
matters, don't make it a bit field.

--
Ben.

Keith Thompson

unread,
Apr 14, 2008, 12:42:00 PM4/14/08
to
Ben Bacarisse <ben.u...@bsb.me.uk> writes:
[...]

> [1] Can we not permit the phrase "global variable" into c.l.c to mean
> an object defined at file scope with external linkage? It would save
> a lot of typing.
[...]

That works for me *if* we expend just a little extra typing to make
clear how the term is being used, since some people do use the term
with subtly different meanings.

Hypothetical example:

Q> I've heard that global variables are a bad idea, but I think
Q> they're really cool.

A> Assuming that "global variables" refers to "objects defined at file
A> scope with external linkage", here's why they're usually a bad idea
A> ...

(I think of "global variables" as a general programming concept, with
"objects defined at file scope with external linkage" being the way to
implement that concept in C. It's analogous to the use of pointer
parameters (a C construct) used to implement pass-by-reference (a more
general programming concept).)

Note that the above still assumes that a const-qualified object is a
"variable", even though it's not able to vary. I'm willing to ignore
that quibble as long as it's clearly not relevant to the particular
case being discussed.

Note also that the use of the term "global" can tend to obscure the
important distinction between scope and storage duration.

Even though C doesn't define either "global" or "variable", I don't
mind using either or both as a convenient shorthand *as long as* the
usage is unambiguous (or at least as unambiguous as it needs to be).

--
Keith Thompson (The_Other_Keith) <ks...@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Richard

unread,
Apr 14, 2008, 1:35:11 PM4/14/08
to

Keith Thompson <ks...@mib.org> writes:

> Even though C doesn't define either "global" or "variable", I don't
> mind using either or both as a convenient shorthand *as long as* the
> usage is unambiguous (or at least as unambiguous as it needs to be).

What a load of hot air.

It should be apparent to an sentient being what a "variable" is in a
programming newsgroup. Even this one. Ditto for "global".


lawrenc...@siemens.com

unread,
Apr 14, 2008, 5:38:00 PM4/14/08
to
Richard <de...@gmail.com> wrote:
>
> It should be apparent to an sentient being what a "variable" is in a
> programming newsgroup. Even this one. Ditto for "global".

"Variable" shouldn't be controversial, even the C Standard uses the
term. But "global variable" is a slipperier concept in C. For example,
does a file scope static variable qualify or not? It's not exactly a
local variable, but it's not exactly a global variable, either.

-Larry Jones

OK, there IS a middle ground, but it's for sissy weasels. -- Calvin

Ben Bacarisse

unread,
Apr 14, 2008, 7:17:40 PM4/14/08
to
Richard <de...@gmail.com> writes:

> lawrenc...@siemens.com writes:
>
>> Richard <de...@gmail.com> wrote:
>>>
>>> It should be apparent to an sentient being what a "variable" is in a
>>> programming newsgroup. Even this one. Ditto for "global".
>>
>> "Variable" shouldn't be controversial, even the C Standard uses the
>> term. But "global variable" is a slipperier concept in C. For example,
>> does a file scope static variable qualify or not? It's not exactly a
>

> No. Because its file scope. Not global scope. I don think I'm being
> overly simplistic here.

You (correctly in my opinion) exclude it, but you should exclude it
because it does not have external linkage. This variable:

int x;

outside of any function is also a file scape object, and you probably
*do* call that a global.

--
Ben.

Richard

unread,
Apr 14, 2008, 6:24:44 PM4/14/08
to
lawrenc...@siemens.com writes:

> Richard <de...@gmail.com> wrote:
>>
>> It should be apparent to an sentient being what a "variable" is in a
>> programming newsgroup. Even this one. Ditto for "global".
>
> "Variable" shouldn't be controversial, even the C Standard uses the
> term. But "global variable" is a slipperier concept in C. For example,
> does a file scope static variable qualify or not? It's not exactly a

No. Because its file scope. Not global scope. I don think I'm being
overly simplistic here. But I tire of the usual suspects in this NG
playing games with language in order to show off their knowledge of
standard. Everyone in the programming world knows what is meant by a
global variable. And Heathfield's claim that C doesn't have them is
total and utter hogwash.

> local variable, but it's not exactly a global variable, either.

The scope of the variable is local. The fact that its not an automatic
variable is another issue.

Ben Bacarisse

unread,
Apr 14, 2008, 7:10:37 PM4/14/08
to
lawrenc...@siemens.com writes:

> Richard <de...@gmail.com> wrote:
>>
>> It should be apparent to an sentient being what a "variable" is in a
>> programming newsgroup. Even this one. Ditto for "global".
>
> "Variable" shouldn't be controversial, even the C Standard uses the
> term. But "global variable" is a slipperier concept in C. For example,
> does a file scope static variable qualify or not? It's not exactly a
> local variable, but it's not exactly a global variable, either.

It is a grey area, but I did explicitly exclude it.

--
Ben.

Richard Heathfield

unread,
Apr 15, 2008, 12:54:21 AM4/15/08
to
Ben Bacarisse said:

> Richard <de...@gmail.com> writes:
>
>> lawrenc...@siemens.com writes:
>>
>>> Richard <de...@gmail.com> wrote:
>>>>
>>>> It should be apparent to an sentient being what a "variable" is in a
>>>> programming newsgroup. Even this one. Ditto for "global".
>>>
>>> "Variable" shouldn't be controversial, even the C Standard uses the
>>> term. But "global variable" is a slipperier concept in C. For
>>> example,
>>> does a file scope static variable qualify or not? It's not exactly a
>>
>> No. Because its file scope. Not global scope. I don think I'm being
>> overly simplistic here.
>
> You (correctly in my opinion) exclude it, but you should exclude it
> because it does not have external linkage.

Excluding it because it doesn't have global scope is perfectly correct.
"There are four kinds of scopes: function, file, block, and function
prototype", quoth the Standard. Since no object can be considered global,
even by a troll, unless it has global scope, and since there is no such
thing as global scope in C, *therefore* C doesn't have global objects (or
"variables", as some people like to call them). And even the trolls agree
with this.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Nick Keighley

unread,
Apr 15, 2008, 6:22:20 AM4/15/08
to
On 14 Apr, 23:24, Richard <de...@gmail.com> wrote:

> lawrence.jo...@siemens.com writes:
> > Richard <de...@gmail.com> wrote:

> >> It should be apparent to an sentient being what a "variable" is in a
> >> programming newsgroup. Even this one. Ditto for "global".
>
> > "Variable" shouldn't be controversial, even the C Standard uses the
> > term.  But "global variable" is a slipperier concept in C.  For example,
> > does a file scope static variable qualify or not?  It's not exactly a
>
> No. Because its file scope. Not global scope. I don think I'm being
> overly simplistic here. But I tire of the usual suspects in this NG
> playing games with language in order to show off their knowledge of
> standard. Everyone in the programming world knows what is meant by a
> global variable.

I have encountered people who referred to file scope objects as
"global parameters" or "globals".

So when I hear the term "global" I'm liable to ask "how global?".


> And Heathfield's claim that C doesn't have them is
> total and utter hogwash.
>
> > local variable, but it's not exactly a global variable, either.
>
> The scope of the variable is local. The fact that its not an automatic
> variable is another issue.


--
Nick Keighley

Richard Harter

unread,
Apr 15, 2008, 9:58:05 AM4/15/08
to
On Tue, 15 Apr 2008 04:54:21 +0000, Richard Heathfield
<r...@see.sig.invalid> wrote:

>Ben Bacarisse said:
>
>> Richard <de...@gmail.com> writes:
>>
>>> lawrenc...@siemens.com writes:
>>>
>>>> Richard <de...@gmail.com> wrote:
>>>>>
>>>>> It should be apparent to an sentient being what a "variable" is in a
>>>>> programming newsgroup. Even this one. Ditto for "global".
>>>>
>>>> "Variable" shouldn't be controversial, even the C Standard uses the
>>>> term. But "global variable" is a slipperier concept in C. For
>>>> example,
>>>> does a file scope static variable qualify or not? It's not exactly a
>>>
>>> No. Because its file scope. Not global scope. I don think I'm being
>>> overly simplistic here.
>>
>> You (correctly in my opinion) exclude it, but you should exclude it
>> because it does not have external linkage.
>
>Excluding it because it doesn't have global scope is perfectly correct.
>"There are four kinds of scopes: function, file, block, and function
>prototype", quoth the Standard. Since no object can be considered global,
>even by a troll, unless it has global scope, and since there is no such
>thing as global scope in C, *therefore* C doesn't have global objects (or
>"variables", as some people like to call them). And even the trolls agree
>with this.

For some particular breed of troll, of course. Some of us trolls
wouldn't use that definition for global.


Richard Harter, c...@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.

Richard

unread,
Apr 15, 2008, 10:14:54 AM4/15/08
to
c...@tiac.net (Richard Harter) writes:

Heathfield is playing silly games. C has global variables and no amount
of silly word games will change that.

Keith Thompson

unread,
Apr 15, 2008, 12:45:55 PM4/15/08
to
lawrenc...@siemens.com writes:
> Richard <de...@gmail.com> wrote:
>> It should be apparent to an sentient being what a "variable" is in a
>> programming newsgroup. Even this one. Ditto for "global".

Mr. Riley, perhaps you'd care to keep the personal insults to
yourself.

> "Variable" shouldn't be controversial, even the C Standard uses the
> term. But "global variable" is a slipperier concept in C. For example,
> does a file scope static variable qualify or not? It's not exactly a
> local variable, but it's not exactly a global variable, either.

Even "variable" isn't clearly defined -- and the standard only uses it
as an adjective or in an informal (in particular, non-normative)
context. I think we can all agree that a single declared non-const
object is a "variable", and I have no problem using the term
informally in cases like that where it's unambiguous. But what about
a const-declared object? What about a member of a struct or union?
What about an element of an array? What about an object allocated by
malloc()?

Perhaps Richard has a clear concept of whether each of this is a
"variable" or not, but I doubt that everyone would agree on each one
of them.

The fact is that the standard generally refers to "objects" rather
than "variables" because the things it has to say usually apply to all
objects, not just the subset that you might call "variables".

Here's a challenge. Provide an unambiguous definition of the word
"variable", using terms defined in the standard. The definition must
specify, for each of the cases I listed above and for any cases I
haven't mentioned, whether it's a "variable" or not. For extra
credit, provide a definition that everyone will agree on.

Keith Thompson

unread,
Apr 15, 2008, 12:49:44 PM4/15/08
to
Richard <de...@gmail.com> writes:
[...]

> C has global variables and no amount of silly word games will change
> that.

Please provide a complete and unambiguous definition of the phrase
"global variable" using terms defined by the C standard. If you
manage to provide a definition we can all agree on, I won't object to
the use of the term.

Richard Tobin

unread,
Apr 15, 2008, 1:37:03 PM4/15/08
to
In article <871w575...@kvetch.smov.org>,
Keith Thompson <ks...@mib.org> wrote:

>> C has global variables and no amount of silly word games will change
>> that.

>Please provide a complete and unambiguous definition of the phrase
>"global variable" using terms defined by the C standard.

I suggest it is a variable that can, given a suitable declaration, be
made visible anywhere in the program. (If you object that the term
"variable" is not defined, then presumably you will also object
to that word's use in comp.lang.c too.)

So the variable defined by the top-level definition

int x;

is global, because you can declare

extern int x;

in any other file and it will refer to that variable.

The variable defined by the top-level definition

static int x;

on the other hand is not visible in any other file regardless of
the declarations used.

The definition amounts to saying that it is an identifier with
external linkage, but is intended to motivate the use of the term
"global".

-- Richard
--
:wq

Richard Heathfield

unread,
Apr 15, 2008, 1:52:57 PM4/15/08
to
Richard Tobin said:

> In article <871w575...@kvetch.smov.org>,
> Keith Thompson <ks...@mib.org> wrote:
>
>>> C has global variables and no amount of silly word games will change
>>> that.
>
>>Please provide a complete and unambiguous definition of the phrase
>>"global variable" using terms defined by the C standard.
>
> I suggest it is a variable that can, given a suitable declaration, be
> made visible anywhere in the program.

Give a suitable declaration, and I'll show you a part of a C program where
this "variable" is not visible. The technique is obvious:

extern int x;

double foo(void)
{
double x = 3.14;
/* int x is not visible here */
return x;
}

> (If you object that the term
> "variable" is not defined, then presumably you will also object
> to that word's use in comp.lang.c too.)

Right. :-)

Richard

unread,
Apr 15, 2008, 2:12:44 PM4/15/08
to
Keith Thompson <ks...@mib.org> writes:

> Richard <de...@gmail.com> writes:
> [...]
>> C has global variables and no amount of silly word games will change
>> that.
>
> Please provide a complete and unambiguous definition of the phrase
> "global variable" using terms defined by the C standard. If you
> manage to provide a definition we can all agree on, I won't object to
> the use of the term.

I dont care if you object or not. C has global variables. However you
want to obfuscate it by referring to the standard. This group is not C89
you know. Or C99.

It's about C.

Whether you like it or not.

And however I produce them, e.g using extern or not, C has variables
that can be accessed from everywhere in a statically linked program.

To anyone who speaks English this is a global variable.

But then I inhabit the real world where people talk about real things
and not some dusty, confusing paragraph in a standard.


Richard

unread,
Apr 15, 2008, 2:13:37 PM4/15/08
to
ric...@cogsci.ed.ac.uk (Richard Tobin) writes:

I'm surprised you bothered to answer his petty requests. It is obvious
to anyone with an iota of common sense who does not want to score points
through obfuscation and general one upsmanship.

Morris Dovey

unread,
Apr 15, 2008, 3:25:47 PM4/15/08
to
Richard wrote:

> I'm surprised you bothered to answer his petty requests. It is obvious
> to anyone with an iota of common sense who does not want to score points
> through obfuscation and general one upsmanship.

Hmm.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/

Morris Dovey

unread,
Apr 15, 2008, 3:32:33 PM4/15/08
to
Richard <de...@gmail.com> wrote:

<snip don't care>

> But then I inhabit the real world where people talk about real things
> and not some dusty, confusing paragraph in a standard.

Do you really find the standard confusing?

Richard Harter

unread,
Apr 15, 2008, 2:32:23 PM4/15/08
to
On Tue, 15 Apr 2008 09:49:44 -0700, Keith Thompson
<ks...@mib.org> wrote:

>Richard <de...@gmail.com> writes:
>[...]
>> C has global variables and no amount of silly word games will change
>> that.
>
>Please provide a complete and unambiguous definition of the phrase
>"global variable" using terms defined by the C standard. If you
>manage to provide a definition we can all agree on, I won't object to
>the use of the term.

Someone, I can't recall who, it must have been some other Keith,
gave a quite nice definition. Still, one can quibble about
definitions. Stepping outside of C for a moment, there are (or
can conceived of being) two general categories of globals -
program wide globals and module wide globals, and two major
visibility rules - automatically visible and visible only with a
declaration. C doesn't have modules as such but files, er,
translation units play much the same role, and file scope
variables are, in effect, module wide globals. So, C in effect
has two types of globals, program wide globals visible with a
declaration and translation unit wide globals automatically
visible.

As for defining variables, providing a definition using the
language of the C standard is quite beyond. Roughly speaking C
variables are identifiers bound both to a storage declaration and
(potentially) to a storage object. There are corner cases, of
course, but then any adequate description of C is all corner
cases.

Richard Harter

unread,
Apr 15, 2008, 2:54:06 PM4/15/08
to
On Tue, 15 Apr 2008 17:52:57 +0000, Richard Heathfield
<r...@see.sig.invalid> wrote:

>Richard Tobin said:
>
>> In article <871w575...@kvetch.smov.org>,
>> Keith Thompson <ks...@mib.org> wrote:
>>
>>>> C has global variables and no amount of silly word games will change
>>>> that.
>>
>>>Please provide a complete and unambiguous definition of the phrase
>>>"global variable" using terms defined by the C standard.
>>
>> I suggest it is a variable that can, given a suitable declaration, be
>> made visible anywhere in the program.
>
>Give a suitable declaration, and I'll show you a part of a C program where
>this "variable" is not visible. The technique is obvious:
>
>extern int x;
>
>double foo(void)
>{
> double x = 3.14;
> /* int x is not visible here */
> return x;
>}

Some of us trolls think that you're quibbling, and, if quibbling
is in order, you are in the wrong. The key phrase here is "can
be made visible". You can choose to make it visible; in your
example you choose not to.



>
>> (If you object that the term
>> "variable" is not defined, then presumably you will also object
>> to that word's use in comp.lang.c too.)
>
>Right. :-)
>
>
>--
>Richard Heathfield <http://www.cpax.org.uk>
>Email: -http://www. +rjh@
>Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
>"Usenet is a strange place" - dmr 29 July 1999

Richard Harter, c...@tiac.net

Richard Heathfield

unread,
Apr 15, 2008, 3:13:19 PM4/15/08
to
Richard Harter said:

<snip>



> Some of us trolls think that you're quibbling,

Quibbling is sometimes how we get to the truth.

> and, if quibbling
> is in order, you are in the wrong. The key phrase here is "can
> be made visible". You can choose to make it visible; in your
> example you choose not to.

Well, yes, I'm quibbling, but surely this is a matter of which comes first,
the file scope object or the auto object. I accept that, if the file scope
object is there, then inserting a shadowing local is a bit of a fudge. But
if the auto object is *already there*, then that's a legitimate case where
the file scope object *can't* be seen from that point in the program
unless you rename one object or the other. At that point, you're having to
change the existing program in order to shoehorn the file scope object
into visible place.

Keith Thompson

unread,
Apr 15, 2008, 3:24:43 PM4/15/08
to
Richard <de...@gmail.com> writes:
> Keith Thompson <ks...@mib.org> writes:
> > Richard <de...@gmail.com> writes:
> > [...]
> >> C has global variables and no amount of silly word games will change
> >> that.
> >
> > Please provide a complete and unambiguous definition of the phrase
> > "global variable" using terms defined by the C standard. If you
> > manage to provide a definition we can all agree on, I won't object to
> > the use of the term.
>
> I dont care if you object or not. C has global variables. However you
> want to obfuscate it by referring to the standard. This group is not C89
> you know. Or C99.

Referring to the standard is obfuscation? That's a new one.

> It's about C.
>
> Whether you like it or not.
>
> And however I produce them, e.g using extern or not, C has variables
> that can be accessed from everywhere in a statically linked program.
>
> To anyone who speaks English this is a global variable.
>
> But then I inhabit the real world where people talk about real things
> and not some dusty, confusing paragraph in a standard.

I'd be glad to explain any passages that you find confusing.

You think that the term "global variable" is perfectly clear. You may
well be right. But if you're not able to provide a definition, then I
suggest that you don't really understand yourself what it means.

Apparently you think it has something to do with statically linked
programs. I find that surprising. Would you care to elaborate on
that point?

If you do choose to offer a definition, please consider this case: An
object declared within a function with the "static" keyword can be
accessed from anywhere in a program via its address. For that matter,
an object declared within a function (e.g., within main) *without* the
"static" keyword can likewise be accessed from anywhere in a program
via its address, as long as any such access occurs only within its
lifetime. I presume you don't intend the term "global variable" to
cover these cases.

You're assuming that it's obvious to everyone what a "global variable"
is. Prove it. Start by proving that you know what it is.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>

Antoninus Twink

unread,
Apr 15, 2008, 3:27:34 PM4/15/08
to
On 15 Apr 2008 at 19:13, Richard Heathfield wrote:
> Richard Harter said:
>
><snip>
>
>> Some of us trolls think that you're quibbling,

As time goes by, more and more sensible people feel themselves excluded
as "trolls".

> Quibbling is sometimes how we get to the truth.

If you say so.

extern int x;

double foo(void)
{
int *xp = &x;


double x = 3.14;
/* int x is not visible here */

printf("global x=%d: not visible, but still accessible\n", *xp);
return x;
}

Keith Thompson

unread,
Apr 15, 2008, 3:32:10 PM4/15/08
to
c...@tiac.net (Richard Harter) writes:
> On Tue, 15 Apr 2008 09:49:44 -0700, Keith Thompson
> <ks...@mib.org> wrote:
>
> >Richard <de...@gmail.com> writes:
> >[...]
> >> C has global variables and no amount of silly word games will change
> >> that.
> >
> >Please provide a complete and unambiguous definition of the phrase
> >"global variable" using terms defined by the C standard. If you
> >manage to provide a definition we can all agree on, I won't object to
> >the use of the term.
>
> Someone, I can't recall who, it must have been some other Keith,
> gave a quite nice definition.

Are you suggesting that I've given a definition of "global variable"?
If so, I honestly don't remember it; can you provide a reference (URL
or message-id)?

[...]

> As for defining variables, providing a definition using the
> language of the C standard is quite beyond.

Quite beyond what? I don't understand what you mean by that.

> Roughly speaking C
> variables are identifiers bound both to a storage declaration and
> (potentially) to a storage object. There are corner cases, of
> course, but then any adequate description of C is all corner
> cases.

Your (admittedly rough) definition implies that a variable is an
identifier. I would have thought that a variable is the object that
an identifier refers to.

If "variable" is such a simple concept (and I'm not arguing that it
isn't), then it shouldn't be difficult to define it in standard terms.
And I have no problem using the term informally when there's no
ambiguity. For example, given:

void func(void)
{
int var;
...
}

the object named "var" is clearly a variable. But yes, it's the
corner cases I'm concerned about. And even if we nail down what a
"variable" is, the phrase "global variable" presents even more corner
cases, something that Mr. Riley refuses to acknowledge.

--
Keith Thompson (The_Other_Keith) k...@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"

Keith Thompson

unread,
Apr 15, 2008, 3:42:23 PM4/15/08
to
c...@tiac.net (Richard Harter) writes:
[...]

> Some of us trolls think that you're quibbling, and, if quibbling
> is in order, you are in the wrong.
[...]

<OT>
Richard, I've never thought of you as a troll. Out of curiosity, why
have you started calling yourself one?
</OT>

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>

Willem

unread,
Apr 15, 2008, 3:48:01 PM4/15/08
to
Antoninus wrote:
) If you say so.
)
) extern int x;
)
) double foo(void)
) {
) int *xp = &x;
) double x = 3.14;
) /* int x is not visible here */
) printf("global x=%d: not visible, but still accessible\n", *xp);
) return x;
) }

int *get_x(void)
{
static int x;
return &x;
}

So, I guess this version of x is a global variable as well then ?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Antoninus Twink

unread,
Apr 15, 2008, 4:10:59 PM4/15/08
to
On 15 Apr 2008 at 19:48, Willem wrote:

> Antoninus wrote:
> > extern int x;
> >
> > double foo(void)
> > {
> > int *xp = &x;
> > double x = 3.14;
> > /* int x is not visible here */
> > printf("global x=%d: not visible, but still accessible\n", *xp);
> > return x;

> > }
>
> int *get_x(void)
> {
> static int x;
> return &x;
> }
>
> So, I guess this version of x is a global variable as well then ?

I was debunking Heathfield's word games, not trying to start playing my
own. Being globally accessible is a necessary but not sufficient
condition for being a global variable.

But then, we all know what a global variable is, so this entire
discussion is pointless.

If the pedants really want a definition, how about "a symbol associated
with a variable that occurs in the table of externally-visible symbols
in some object file"?

Richard Harter

unread,
Apr 15, 2008, 4:12:15 PM4/15/08
to
On 15 Apr 2008 12:32:10 -0700, Keith Thompson <k...@cts.com>
wrote:

>c...@tiac.net (Richard Harter) writes:
>> On Tue, 15 Apr 2008 09:49:44 -0700, Keith Thompson
>> <ks...@mib.org> wrote:
>>
>> >Richard <de...@gmail.com> writes:
>> >[...]
>> >> C has global variables and no amount of silly word games will change
>> >> that.
>> >
>> >Please provide a complete and unambiguous definition of the phrase
>> >"global variable" using terms defined by the C standard. If you
>> >manage to provide a definition we can all agree on, I won't object to
>> >the use of the term.
>>
>> Someone, I can't recall who, it must have been some other Keith,
>> gave a quite nice definition.
>
>Are you suggesting that I've given a definition of "global variable"?
>If so, I honestly don't remember it; can you provide a reference (URL
>or message-id)?


I can't find the message - there is simply too much stuff in
c.l.c - and perhaps it was someone else, but the style was yours.
The essence was something like "if you want to define a global as
a file scope variable with extern qualifier then we can talk
about globals in C". Does that sound familiar.

>
>[...]
>
>> As for defining variables, providing a definition using the
>> language of the C standard is quite beyond.
>
>Quite beyond what? I don't understand what you mean by that.

"quite beyond my feeble mind", which is understandable since it
seems that actually typing what I am thinking of is quite beyond
my feeble mind.

>
>> Roughly speaking C
>> variables are identifiers bound both to a storage declaration and
>> (potentially) to a storage object. There are corner cases, of
>> course, but then any adequate description of C is all corner
>> cases.
>
>Your (admittedly rough) definition implies that a variable is an
>identifier. I would have thought that a variable is the object that
>an identifier refers to.

Bad wording on my part - I meant that a variable a three in one
kind of thing, the three parts being bound together. All three
parts are part of the package. That said, it is the contents of
the object that can vary.

>
>If "variable" is such a simple concept (and I'm not arguing that it
>isn't), then it shouldn't be difficult to define it in standard terms.
>And I have no problem using the term informally when there's no
>ambiguity. For example, given:
>
> void func(void)
> {
> int var;
> ...
> }
>
>the object named "var" is clearly a variable. But yes, it's the
>corner cases I'm concerned about. And even if we nail down what a
>"variable" is, the phrase "global variable" presents even more corner
>cases, something that Mr. Riley refuses to acknowledge.

Richard Harter, c...@tiac.net

Keith Thompson

unread,
Apr 15, 2008, 4:31:57 PM4/15/08
to
c...@tiac.net (Richard Harter) writes:
> On 15 Apr 2008 12:32:10 -0700, Keith Thompson <k...@cts.com>
> wrote:
> >c...@tiac.net (Richard Harter) writes:
> >> On Tue, 15 Apr 2008 09:49:44 -0700, Keith Thompson
> >> <ks...@mib.org> wrote:
> >>
> >> >Richard <de...@gmail.com> writes:
> >> >[...]
> >> >> C has global variables and no amount of silly word games will change
> >> >> that.
> >> >
> >> >Please provide a complete and unambiguous definition of the phrase
> >> >"global variable" using terms defined by the C standard. If you
> >> >manage to provide a definition we can all agree on, I won't object to
> >> >the use of the term.
> >>
> >> Someone, I can't recall who, it must have been some other Keith,
> >> gave a quite nice definition.
> >
> >Are you suggesting that I've given a definition of "global variable"?
> >If so, I honestly don't remember it; can you provide a reference (URL
> >or message-id)?
>
> I can't find the message - there is simply too much stuff in
> c.l.c - and perhaps it was someone else, but the style was yours.
> The essence was something like "if you want to define a global as
> a file scope variable with extern qualifier then we can talk
> about globals in C". Does that sound familiar.

Got it. Message-ID <87mynw7...@kvetch.smov.org>, available at
<http://groups.google.com/group/comp.lang.c/msg/02c5c1b781def71e>. It
was actually Ben Bacarisse who proposed the definition.

But I'm not sure that definition is really suitable, for reasons I
mentioned in that article: it means a const-qualified object can be a
global "variable", even though it's not able to vary, and it obscures
the very important distinction between scope and storage duration.

The folks here who insist most loudly that everyone knows what a
"global variable" is are unwilling or unable to offer a definition.
I'll just have to assume, until they demonstrate otherwise, that if
they can't define the term they don't really understand it.

[...]

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>

Richard Harter

unread,
Apr 15, 2008, 4:42:41 PM4/15/08
to
On Tue, 15 Apr 2008 19:13:19 +0000, Richard Heathfield
<r...@see.sig.invalid> wrote:

>Richard Harter said:
>
><snip>
>
>> Some of us trolls think that you're quibbling,
>
>Quibbling is sometimes how we get to the truth.
>
>> and, if quibbling
>> is in order, you are in the wrong. The key phrase here is "can
>> be made visible". You can choose to make it visible; in your
>> example you choose not to.
>
>Well, yes, I'm quibbling, but surely this is a matter of which comes first,
>the file scope object or the auto object. I accept that, if the file scope
>object is there, then inserting a shadowing local is a bit of a fudge. But
>if the auto object is *already there*, then that's a legitimate case where
>the file scope object *can't* be seen from that point in the program
>unless you rename one object or the other. At that point, you're having to
>change the existing program in order to shoehorn the file scope object
>into visible place.

As Twink points out (in case you have him killfiled) the "global"
is still potentially accessible via a pointer even though it is
shadowed. One can even make the argument that so doing is "the
right thing to do", the idea being that a function should not
directly reference variables not within its local scope.

The issue you raise, while important, IMO is a separate one, that
of name space conflict. By this I mean WADQ (With All Due
Qualifications) that a name in the immediate scope can conflict
with a name outside the immediate scope. There are several ways
in C that such conflicts can arise, e.g. duplicated function
names in different files. The possibility of name space conflict
is always a potential source of problems when combining code.

Richard Tobin

unread,
Apr 15, 2008, 4:51:19 PM4/15/08
to
In article <TfKdnaQX1ak...@bt.com>,
Richard Heathfield <r...@see.sig.invalid> wrote:

>Give a suitable declaration, and I'll show you a part of a C program where
>this "variable" is not visible. The technique is obvious:

I thought of mentioning that, but I really thought that everyone, even
in this newsgroup, would take the obvious interpretation.

-- Richard
--
:wq

Richard Harter

unread,
Apr 15, 2008, 5:10:09 PM4/15/08
to
On 15 Apr 2008 12:42:23 -0700, Keith Thompson <k...@cts.com>
wrote:

>c...@tiac.net (Richard Harter) writes:


>[...]
>> Some of us trolls think that you're quibbling, and, if quibbling
>> is in order, you are in the wrong.
>[...]
>
><OT>
>Richard, I've never thought of you as a troll. Out of curiosity, why
>have you started calling yourself one?
></OT>

It's a "poking a little fun at Heathfield" thing, referring to a
dogmatic passage in one of his posts. I'll be good and stop.

As a side note, by my lights Twink et al are not trolls - they
are critics. A troll proper posts rubbish with the object of
raising controversy. The critics genuinely object to the c.l.c
culture. My objection to them is that their criticism is
inconsequential and ineffective. In consequence they are as much
a waste of bandwidth as the net-nannies.

lawrenc...@siemens.com

unread,
Apr 15, 2008, 5:47:47 PM4/15/08
to
Keith Thompson <k...@cts.com> wrote:
>
> You think that the term "global variable" is perfectly clear. You may
> well be right. But if you're not able to provide a definition, then I
> suggest that you don't really understand yourself what it means.

It's apparently like pornography -- hard to define, but you know it when
you see it. :-)

-Larry Jones

What this games needs are negotiated settlements. -- Calvin

ymun...@gmail.com

unread,
Apr 15, 2008, 6:31:33 PM4/15/08
to
On Apr 14, 11:54 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
> Ben Bacarisse said:
>
>
>
> > Richard <de...@gmail.com> writes:
>
> >> lawrence.jo...@siemens.com writes:
>
> >>> Richard <de...@gmail.com> wrote:
>
> >>>> It should be apparent to an sentient being what a "variable" is in a
> >>>> programming newsgroup. Even this one. Ditto for "global".
>
> >>> "Variable" shouldn't be controversial, even the C Standard uses the
> >>> term. But "global variable" is a slipperier concept in C. For
> >>> example,
> >>> does a file scope static variable qualify or not? It's not exactly a
>
> >> No. Because its file scope. Not global scope. I don think I'm being
> >> overly simplistic here.
>
> > You (correctly in my opinion) exclude it, but you should exclude it
> > because it does not have external linkage.
>
> Excluding it because it doesn't have global scope is perfectly correct.
> "There are four kinds of scopes: function, file, block, and function
> prototype", quoth the Standard. Since no object can be considered global,
> even by a troll, unless it has global scope, and since there is no such
> thing as global scope in C, *therefore* C doesn't have global objects (or
> "variables", as some people like to call them).

What scope do allocated objects have? Are you sure you
are not confusing objects and identifiers? "Variable"
is not equivalent to "object" even if you say so ("some
people...", yeah), you're just playing stupid word games
here.

The only argument you can present is: the standard doesn't
define "global variables". Many people will disagree that
this argument is worth a shit, even you are trying to back
it up by some nonsense. Nevertheless, it's a valid argument
and one may say that in technical discussions it's important
not to use ambiguous terms, yadda yadda. Is it what you're
saying? Doesn't look so, more it looks like someone is
trying to be a smart ass without doing his homework. Or
do you really think that "global variable" term is invented
by morons?

Yevgen

Richard Harter

unread,
Apr 15, 2008, 6:36:14 PM4/15/08
to
On 15 Apr 2008 13:31:57 -0700, Keith Thompson <k...@cts.com>
wrote:

Mea culpa. Ben has now been promoted to a Keith.


>
>But I'm not sure that definition is really suitable, for reasons I
>mentioned in that article: it means a const-qualified object can be a
>global "variable", even though it's not able to vary, and it obscures
>the very important distinction between scope and storage duration.

Variables, despite the name, don't have to vary. I don't agree
that the term "global" blurs the distinction; a global, assuming
that we agree on its usage in the context of C, is a fairly well
defined thing that has a definite kind of scope and a definite
kind of storage.


>
>The folks here who insist most loudly that everyone knows what a
>"global variable" is are unwilling or unable to offer a definition.
>I'll just have to assume, until they demonstrate otherwise, that if
>they can't define the term they don't really understand it.

You can assume that if you like, of course. I, on the hand, can
appreciate that they might think it quite obvious what is meant
by the term, and think the various objections are mere
pettigfoggery. I might even agree with them; that said, I agree
(I think) that the discussion would be all the better for
definitions.

I like Ben's definition, modulo corner cases. It captures what
people usually mean by globals.

Keith Thompson

unread,
Apr 15, 2008, 6:41:02 PM4/15/08
to
c...@tiac.net (Richard Harter) writes:
[...]
> As a side note, by my lights Twink et al are not trolls - they
> are critics. A troll proper posts rubbish with the object of
> raising controversy. The critics genuinely object to the c.l.c
> culture. My objection to them is that their criticism is
> inconsequential and ineffective. In consequence they are as much
> a waste of bandwidth as the net-nannies.

Hmm. You may be right, but I don't see it that way. Personally, I
don't give them that much credit. I don't believe they genuinely
object to the culture, or if they do, I don't believe that's the
primary motivation behind their actions. But in any case, I don't
particularly care what they think about the culture. As far as I can
tell, they're just being obnoxious jerks because they think it's fun.

Message has been deleted

istil...@gmail.com

unread,
Apr 15, 2008, 7:02:06 PM4/15/08
to
I have several further questions on global variables.

When are they used? I.e., in what situations it is good to use them.
People are talking about avoiding using them. If so, why creating
global
variables? They must be useful in certain circumstances.

Keith Thompson

unread,
Apr 15, 2008, 7:12:06 PM4/15/08
to
ymun...@gmail.com writes:
[...]

> What scope do allocated objects have? Are you sure you
> are not confusing objects and identifiers? "Variable"
> is not equivalent to "object" even if you say so ("some
> people...", yeah), you're just playing stupid word games
> here.
>
> The only argument you can present is: the standard doesn't
> define "global variables". Many people will disagree that
> this argument is worth a shit, even you are trying to back
> it up by some nonsense. Nevertheless, it's a valid argument
> and one may say that in technical discussions it's important
> not to use ambiguous terms, yadda yadda. Is it what you're
> saying? Doesn't look so, more it looks like someone is
> trying to be a smart ass without doing his homework. Or
> do you really think that "global variable" term is invented
> by morons?

Try again without the personal abusive, and somebody might take you
seriously.

Ian Collins

unread,
Apr 15, 2008, 7:13:02 PM4/15/08
to
istil...@gmail.com wrote:
> I have several further questions on global variables.
>
> When are they used? I.e., in what situations it is good to use them.

Hardly any.

> People are talking about avoiding using them. If so, why creating
> global
> variables? They must be useful in certain circumstances.

C carries a lot of historical baggage. Everywhere a global can be used,
an alternative approach can be used.

--
Ian Collins.

istil...@gmail.com

unread,
Apr 15, 2008, 7:40:31 PM4/15/08
to
On Apr 15, 7:13 pm, Ian Collins <ian-n...@hotmail.com> wrote:

> Everywhere a global can be used,
> an alternative approach can be used.

Good to know that. Could you give a few concrete examples, beside the
"silent" one?

istil...@gmail.com

unread,
Apr 15, 2008, 8:15:52 PM4/15/08
to
On Apr 12, 1:16 am, Ian Collins <ian-n...@hotmail.com> wrote:
If you simply want to remove the global, make silent static
> and expose it through a getSilent() function.

Following your suggestion, I wrote

main.c

static int silent; /* global in main.c */
int getSilent()
{
return silent;
}

int main(int argc, char *argv[])
{
if (some condition)
silent = 1;
}

----

other.c

How to call getSilent()? The compiler would warn "undefined
getSilent()".

Ian Collins

unread,
Apr 15, 2008, 8:20:13 PM4/15/08
to
Place the function declaration in a shared header. That way, you can
expose the value of silent and restrict write access to the file where
you define it.

--
Ian Collins.

ymun...@gmail.com

unread,
Apr 15, 2008, 9:27:54 PM4/15/08
to
On Apr 15, 6:12 pm, Keith Thompson <k...@cts.com> wrote:

> ymunt...@gmail.com writes:
>
> [...]
>
>
>
> > What scope do allocated objects have? Are you sure you
> > are not confusing objects and identifiers? "Variable"
> > is not equivalent to "object" even if you say so ("some
> > people...", yeah), you're just playing stupid word games
> > here.
>
> > The only argument you can present is: the standard doesn't
> > define "global variables". Many people will disagree that
> > this argument is worth a shit, even you are trying to back
> > it up by some nonsense. Nevertheless, it's a valid argument
> > and one may say that in technical discussions it's important
> > not to use ambiguous terms, yadda yadda. Is it what you're
> > saying? Doesn't look so, more it looks like someone is
> > trying to be a smart ass without doing his homework. Or
> > do you really think that "global variable" term is invented
> > by morons?
>
> Try again without the personal abusive, and somebody might take you
> seriously.

Try again what exactly? Tell that there are global
variables? They hardly need my opinion to exist.
Or tell that Richard Heathfiled is playing word games?
That's a personal abusive, I think. Indeed, my words
taken without what they refer to do look horrible,
I am ashamed and all that. Snipping is a good thing.

Meanwhile, comp.lang.c is continuing to discuss the
color of green onion. Try to fin ten differences
between the following two statements: "there are
no global variables in C" and "C++ is faster than C".

Yevgen

Ben Bacarisse

unread,
Apr 15, 2008, 9:47:37 PM4/15/08
to
c...@tiac.net (Richard Harter) writes:

> On 15 Apr 2008 13:31:57 -0700, Keith Thompson <k...@cts.com>
> wrote:

<snip>


>>Got it. Message-ID <87mynw7...@kvetch.smov.org>, available at
>><http://groups.google.com/group/comp.lang.c/msg/02c5c1b781def71e>. It
>>was actually Ben Bacarisse who proposed the definition.

Thank you for finding it. I'd have owned up, but I was busy.

<snip>


> I like Ben's definition, modulo corner cases. It captures what
> people usually mean by globals.

The original intent was to propose a definition of the /term/ not the
/concept/ -- there would be no corner cases! Of course, it is not an
accident that it matches what people usually mean, but the idea was
that, by defining the term, it could be used here without causing a
thread to explode.

Of course, if someone comes here and asks "what is a global
variable?", they are asking about the concept, so it (the concept) has
to be discussed, rather than simply trotting out any canned
definition.

--
Ben.

istil...@gmail.com

unread,
Apr 15, 2008, 9:48:50 PM4/15/08
to
On Apr 15, 8:20 pm, Ian Collins <ian-n...@hotmail.com> wrote:

Suppose I have 100 functions in my program, is it a good idea to put
all their declarations in a header file called proto.h?

Another way maybe putting them into separate .h files, i.e., putting
all function declarations defined in a.c into a.h, all function
declarations defined in b.c into b.h, and so on.
But including a main.h (containing getSilent()) for every .c is not a
good idea.


Ian Collins

unread,
Apr 15, 2008, 9:57:44 PM4/15/08
to
istil...@gmail.com wrote:
> On Apr 15, 8:20 pm, Ian Collins <ian-n...@hotmail.com> wrote:

>> Place the function declaration in a shared header. That way, you can
>> expose the value of silent and restrict write access to the file where
>> you define it.
>>

*Please* don't quote signatures.


>
> Suppose I have 100 functions in my program, is it a good idea to put
> all their declarations in a header file called proto.h?
>

Not really, why would you? If all of those functions form a public
interface, you might want to exposes them. Mode likely...

> Another way maybe putting them into separate .h files, i.e., putting
> all function declarations defined in a.c into a.h, all function
> declarations defined in b.c into b.h, and so on.

...you would want to group your non-static functions in a number of headers.

> But including a main.h (containing getSilent()) for every .c is not a
> good idea.
>

Why not? The name's a bit naff, but putting all your debug related
declarations in something like "debug.h" and including it where required
is perfectly sound practice.

--
Ian Collins.

Ben Bacarisse

unread,
Apr 15, 2008, 10:11:10 PM4/15/08
to
istil...@gmail.com writes:

Do you mean 100 functions like getSilent, or 100 functions of a more
general sort? If you mean the former, I think it indicates your
program could benefit from some re-organisation.

If you just mean 100 functions that are being offered to the other
parts of the program, then I suggest you try to group them into
manageable "topics" -- logging functions, statistical functions, tree
functions, I/O, etc. with a .h (and .c for each group).

> Another way maybe putting them into separate .h files, i.e., putting
> all function declarations defined in a.c into a.h, all function
> declarations defined in b.c into b.h, and so on.

That is a very good way to do it. Especially if the names convey the
related nature of the functions.

> But including a main.h (containing getSilent()) for every .c is not a
> good idea.

There is no shame in that (again, subject to a proper name). If you
group the functions together well, you will often find that each
header file is included by only a few modu^H^H^H^H translation units.
In fact, this is often a sign that you have to the design right -- you
have minimised the coupling between the parts of the program.

--
Ben.

istil...@gmail.com

unread,
Apr 15, 2008, 10:20:18 PM4/15/08
to
On Apr 15, 9:57 pm, Ian Collins <ian-n...@hotmail.com> wrote:

> > But including a main.h (containing getSilent()) for every .c is not a
> > good idea.
>
> Why not? The name's a bit naff, but putting all your debug related
> declarations in something like "debug.h" and including it where required
> is perfectly sound practice.
>

If I only want to use getSilent() defined in main.c, can I just write
a line on top of other.c,

int getSilent()

to let compilation pass?

This way, I don't need to create main.h just for using getSilent()

Ian Collins

unread,
Apr 15, 2008, 10:25:05 PM4/15/08
to

You could, but what's wrong with a header?

--
Ian Collins.

istil...@gmail.com

unread,
Apr 15, 2008, 10:29:38 PM4/15/08
to
On Apr 15, 10:25 pm, Ian Collins <ian-n...@hotmail.com> wrote:

Nothing wrong. Thanks.

Richard Heathfield

unread,
Apr 16, 2008, 1:16:07 AM4/16/08
to
istil...@gmail.com said:

> I have several further questions on global variables.
>
> When are they used?

Firstly, what do you mean by "global variable"? Do you mean file scope
objects of any kind? File scope objects with external linkage? File scope
objects to which you expose update rights over a wide area network?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Richard Heathfield

unread,
Apr 16, 2008, 1:24:43 AM4/16/08
to
ymun...@gmail.com said:

<snip>

> "Variable" is not equivalent to "object" even if you say so

Okay, what *is* it equivalent to, in the C language? Please justify your
answer with reference to the language definition.

> ("some
> people...", yeah), you're just playing stupid word games
> here.
>
> The only argument you can present is: the standard doesn't
> define "global variables".

That's a pretty solid argument. Can you defeat it with reference to the C
Standard?

> Many people will disagree that
> this argument is worth a shit,

And I care how?

> even you are trying to back
> it up by some nonsense.

The C Standard is nonsense now?

> Nevertheless, it's a valid argument
> and one may say that in technical discussions it's important
> not to use ambiguous terms, yadda yadda.

Right. So what *do* you mean by "global variable"? Nobody, as far as I can
see, has yet disambiguated this term to everyone's satisfaction. Until
this is done, the term "global variable" remains ambiguous, and therefore
should not be used without at the very least being defined by the user on
each first usage within an article.

> Is it what you're
> saying? Doesn't look so, more it looks like someone is
> trying to be a smart ass without doing his homework.

Gosh.

> Or do you really think that "global variable" term is invented
> by morons?

If you really think I think that, it says a lot more about you than it does
about me.

ymun...@gmail.com

unread,
Apr 16, 2008, 1:58:47 AM4/16/08
to
On Apr 16, 12:24 am, Richard Heathfield <r...@see.sig.invalid> wrote:
> ymunt...@gmail.com said:
>
> <snip>

You snipped the most important part here. Your "proof"
and what was wrong with that "proof", namely that you
are freely playing with "object" and "scope" and "variable"
words, to make it look like the standard itself somehow
says there are no global variables.

> > "Variable" is not equivalent to "object" even if you say so
>
> Okay, what *is* it equivalent to, in the C language? Please justify your
> answer with reference to the language definition.

As far as I know, the C standard does not define "variable".
It does use that term, and the meaning is, apparently, what
"everybody means" by that. Naturally I can't justify my answer
with the reference to blah blah blah. But why should I?
I didn't say it's something from the C standard. Straw man.

>
> > ("some
> > people...", yeah), you're just playing stupid word games
> > here.
>
> > The only argument you can present is: the standard doesn't
> > define "global variables".
>
> That's a pretty solid argument. Can you defeat it with reference to the C
> Standard?

Of course I can't. Did I say I can? Straw man.

>
> > Many people will disagree that
> > this argument is worth a shit,
>
> And I care how?

Sure, you don't give a shit to what many other
people think. That's natural, and I didn't
say you should or you do care.

> > even you are trying to back
> > it up by some nonsense.
>
> The C Standard is nonsense now?

Straw man. I didn't say the C standard is nonsense.
Your "arguments" were nonsense. That you used words
which appear in the standard doesn't mean you backed
something up by the standard.

>
> > Nevertheless, it's a valid argument
> > and one may say that in technical discussions it's important
> > not to use ambiguous terms, yadda yadda.
>
> Right. So what *do* you mean by "global variable"? Nobody, as far as I can
> see, has yet disambiguated this term to everyone's satisfaction. Until
> this is done, the term "global variable" remains ambiguous,

True. This one is very clear. Who argues about it?

> and therefore
> should not be used without at the very least being defined by the user on
> each first usage within an article.

Perhaps. I wouldn't agree, but it's reasonable. But you
didn't say that. What you did say was that there are no
global variables in C. And that's bullshit.

>
> > Is it what you're
> > saying? Doesn't look so, more it looks like someone is
> > trying to be a smart ass without doing his homework.
>
> Gosh.

What? Read what you snipped again, about scopes of objects.
It's your business to "do research" and whatnot, you're the
smart folk who know the language definition here.

>
> > Or do you really think that "global variable" term is invented
> > by morons?
>
> If you really think I think that, it says a lot more about you than it does
> about me.

Of course I don't think you think that, no, I think
worse things. See, people do use the term without
great problems. Many people, not only those who cast
malloc() result or write void main(). But there is
*one* person who claims plain and clear that there
are no global variables in C, and proves (!) that
using the standard.

It's similar to the following: sockets are off-topic,
the C standard doesn't define sockets. If you are
saying that we shouldn't talk about sockets in
comp.lang.c, it is reasonable. Some may agree, some
not, and so on.

But, if you "prove", using the C standard. that it's
impossible to write a C program which uses sockets,
then what's the word to call you?

Yevgen

ymun...@gmail.com

unread,
Apr 16, 2008, 2:02:30 AM4/16/08
to
On Apr 16, 12:16 am, Richard Heathfield <r...@see.sig.invalid> wrote:

> istillsh...@gmail.com said:
>
> > I have several further questions on global variables.
>
> > When are they used?
>
> Firstly, what do you mean by "global variable"? Do you mean file scope
> objects of any kind? File scope objects with external linkage?

Could you define what "file scope objects" mean please?
With reference to the language definition of course.

> File scope
> objects to which you expose update rights over a wide area network?

Do you think the OP could mean that?

Yevgen

Richard Heathfield

unread,
Apr 16, 2008, 2:11:52 AM4/16/08
to
ymun...@gmail.com said:

> On Apr 16, 12:24 am, Richard Heathfield <r...@see.sig.invalid> wrote:
>> ymunt...@gmail.com said:
>>
>> <snip>
>
> You snipped the most important part here. Your "proof"
> and what was wrong with that "proof", namely that you
> are freely playing with "object" and "scope" and "variable"
> words, to make it look like the standard itself somehow
> says there are no global variables.

The Standard does NOT say there are no global variables, and I never
claimed that it does. The Standard does NOT say that there are no
leprechauns, either. So what?

>> > "Variable" is not equivalent to "object" even if you say so
>>
>> Okay, what *is* it equivalent to, in the C language? Please justify your
>> answer with reference to the language definition.
>
> As far as I know, the C standard does not define "variable".

Right.

> It does use that term, and the meaning is, apparently, what
> "everybody means" by that.

Which is what?

As long as you continue to evade this point, you don't have an argument.
And the moment you define what you mean, you'll find that some people
disagree with you, so it turns out that what "everybody means" by that is
*not* in fact what everybody means by that. Resolving such an impasse is
the reason we have Standards in the first place.

<nonsense snipped>

Richard Heathfield

unread,
Apr 16, 2008, 2:21:16 AM4/16/08
to
ymun...@gmail.com said:

> On Apr 16, 12:16 am, Richard Heathfield <r...@see.sig.invalid> wrote:
>> istillsh...@gmail.com said:
>>
>> > I have several further questions on global variables.
>>
>> > When are they used?
>>
>> Firstly, what do you mean by "global variable"? Do you mean file scope
>> objects of any kind? File scope objects with external linkage?
>
> Could you define what "file scope objects" mean please?

When I use the term "file scope objects", I am referring to objects that
have file scope.

> With reference to the language definition of course.

See, for example, 3.7.2 which refers to "an object that has file scope"
(6.9.2(2) in C99).

Nevertheless, the Standard more usually refers to identifiers with file
scope, and "file scope object" is a rather lazy shorthand for "object
identified by identifiers with file scope". Can you come up with another
reasonable interpretation for "file scope object" that would render the
term sufficiently ambiguous to be unusable? If you *can*, you win my
argument for me. And if not, what are you complaining about?

>> File scope
>> objects to which you expose update rights over a wide area network?
>
> Do you think the OP could mean that?

Do you think the OP *must* mean whatever it is that you mean by the term
"global variables"? If he doesn't mean the same thing as you, is he wrong?
If he *is* wrong, can you explain why?

ymun...@gmail.com

unread,
Apr 16, 2008, 3:33:12 AM4/16/08
to
On Apr 16, 1:11 am, Richard Heathfield <r...@see.sig.invalid> wrote:
> ymunt...@gmail.com said:
>
> > On Apr 16, 12:24 am, Richard Heathfield <r...@see.sig.invalid> wrote:
> >> ymunt...@gmail.com said:
>
> >> <snip>
>
> > You snipped the most important part here. Your "proof"
> > and what was wrong with that "proof", namely that you
> > are freely playing with "object" and "scope" and "variable"
> > words, to make it look like the standard itself somehow
> > says there are no global variables.
>
> The Standard does NOT say there are no global variables, and I never
> claimed that it does. The Standard does NOT say that there are no
> leprechauns, either. So what?

Exactly. That's why it's so ridiculous when you "prove"
that there are no global variables.

> >> > "Variable" is not equivalent to "object" even if you say so
>
> >> Okay, what *is* it equivalent to, in the C language? Please justify your
> >> answer with reference to the language definition.
>
> > As far as I know, the C standard does not define "variable".
>
> Right.
>
> > It does use that term, and the meaning is, apparently, what
> > "everybody means" by that.
>
> Which is what?
>
> As long as you continue to evade this point, you don't have an argument.

It wasn't the point, and you understand it pretty well.
I did say that the term was ambiguous, I used quotes
for "everybody means" to emphasize that it doesn't literally
mean "what everybody means", and so on. But since you
can't argue about what I really was talking about, all
you can do is to pull a straw man. "Global variable"
is not defined, it is ambiguous - here, I repeated it
(not the second time even). Does it change anything?
Nope.

> And the moment you define what you mean, you'll find that some people
> disagree with you, so it turns out that what "everybody means" by that is
> *not* in fact what everybody means by that. Resolving such an impasse is
> the reason we have Standards in the first place.
>
> <nonsense snipped>

Yep, snip snip and you win the argument.

Yevgen

ymun...@gmail.com

unread,
Apr 16, 2008, 3:40:26 AM4/16/08
to
On Apr 16, 1:21 am, Richard Heathfield <r...@see.sig.invalid> wrote:

> ymunt...@gmail.com said:
>
> > On Apr 16, 12:16 am, Richard Heathfield <r...@see.sig.invalid> wrote:
> >> istillsh...@gmail.com said:
>
> >> > I have several further questions on global variables.
>
> >> > When are they used?
>
> >> Firstly, what do you mean by "global variable"? Do you mean file scope
> >> objects of any kind? File scope objects with external linkage?
>
> > Could you define what "file scope objects" mean please?
>
> When I use the term "file scope objects", I am referring to objects that
> have file scope.
>
> > With reference to the language definition of course.
>
> See, for example, 3.7.2 which refers to "an object that has file scope"
> (6.9.2(2) in C99).

6.9.2p2 refers to an *identifier* which has file scope.
Identifiers have scope, that's right. Now, I won't be
surprised too much if the standard does say "scope of
object" somewhere, it's a big document edited during
many years; but it does not define "scope of object".

>
> Nevertheless, the Standard more usually refers to identifiers with file
> scope, and "file scope object" is a rather lazy shorthand for "object
> identified by identifiers with file scope".

I do understand what you mean (I think). But we need
a definition you see, so nobody is confused, etc.

> Can you come up with another
> reasonable interpretation for "file scope object" that would render the
> term sufficiently ambiguous to be unusable?

Of course I can't. Can you come up with a reasonable
interpretation of "global variable" that would render
the term sufficiently ambiguous to be unusable? That
it's ambigous doesn't make it unusable at all. Except
for you, of course. Since "global variable" may refer
to "objects to which you expose update rights over
a wide area network".

> If you *can*, you win my
> argument for me. And if not, what are you complaining about?
>
> >> File scope
> >> objects to which you expose update rights over a wide area network?
>
> > Do you think the OP could mean that?
>
> Do you think the OP *must* mean whatever it is that you mean by the term
> "global variables"? If he doesn't mean the same thing as you, is he wrong?
> If he *is* wrong, can you explain why?

So, do you think the OP could mean that or not?

Yevgen

Richard Heathfield

unread,
Apr 16, 2008, 3:57:27 AM4/16/08
to
ymun...@gmail.com said:

> On Apr 16, 1:11 am, Richard Heathfield <r...@see.sig.invalid> wrote:

<snip>

>> The Standard does NOT say there are no global variables, and I never
>> claimed that it does. The Standard does NOT say that there are no
>> leprechauns, either. So what?
>
> Exactly. That's why it's so ridiculous when you "prove"
> that there are no global variables.

I can't prove a negative, and I don't plan to try. What I'd like to see is
a definition that everyone agrees on, and then some evidence that there
*are* global variables in C, but you seem loathe to provide either the
definition or the evidence.

<snip>

>> > It does use that term, and the meaning is, apparently, what
>> > "everybody means" by that.
>>
>> Which is what?
>>
>> As long as you continue to evade this point, you don't have an argument.
>
> It wasn't the point, and you understand it pretty well.

It is *a* point, and one that you are still trying to evade.

> I did say that the term was ambiguous,

Right. So if we don't say what we mean by it, we risk being misunderstood -
and therefore the term is not *useful*, at least without defining it
whenever we first use it in an article.

> I used quotes
> for "everybody means" to emphasize that it doesn't literally
> mean "what everybody means", and so on.

If you know that not everyone means the same thing by the term, it seems
strange that you should bother to use the phrase at all, quotes or no
quotes. The lack of a universal definition hampers the usefulness of the
term - irrecoverably, in my view.

> But since you
> can't argue about what I really was talking about,

No, I can't, because you *WON'T TELL ME* what you are really talking about.
You say "global variable" but won't say WHAT IT IS. What's the big secret
here?

> all
> you can do is to pull a straw man. "Global variable"
> is not defined,

Right.

> it is ambiguous

Right.

> - here, I repeated it
> (not the second time even). Does it change anything?
> Nope.

Right. You have an undefined, ambiguous term - i.e. a useless term.

>> And the moment you define what you mean, you'll find that some people
>> disagree with you, so it turns out that what "everybody means" by that
>> is *not* in fact what everybody means by that. Resolving such an impasse
>> is the reason we have Standards in the first place.
>>
>> <nonsense snipped>
>
> Yep, snip snip and you win the argument.

Who gives a damn about the argument? If the term "global variable" somehow
adds to our ability to discuss C in this group, perhaps by - in some small
way - removing confusion between two similar concepts, then let's use it
by all means. But surely if that's going to happen, we have to know what
the term actually means! Nobody has yet provided a definition that we're
all happy with, and the Standard is of no help because it doesn't itself
provide a definition. And neither will you. Why not? What's the big
secret?

Richard Heathfield

unread,
Apr 16, 2008, 3:59:21 AM4/16/08
to
ymun...@gmail.com said:

> On Apr 16, 1:21 am, Richard Heathfield <r...@see.sig.invalid> wrote:

<snip>

>> Can you come up with another
>> reasonable interpretation for "file scope object" that would render the
>> term sufficiently ambiguous to be unusable?
>
> Of course I can't.

Then what are you complaining about?

> Can you come up with a reasonable
> interpretation of "global variable" that would render
> the term sufficiently ambiguous to be unusable?

You start - give me *one* definition of "global variable".

<snip>

ymun...@gmail.com

unread,
Apr 16, 2008, 4:03:41 AM4/16/08
to
On Apr 16, 2:59 am, Richard Heathfield <r...@see.sig.invalid> wrote:

> ymunt...@gmail.com said:
>
> > On Apr 16, 1:21 am, Richard Heathfield <r...@see.sig.invalid> wrote:
>
> <snip>
>
> >> Can you come up with another
> >> reasonable interpretation for "file scope object" that would render the
> >> term sufficiently ambiguous to be unusable?
>
> > Of course I can't.
>
> Then what are you complaining about?

All right, you won. You are right as always. Right amount
of snipping, and it's no longer obvious that it was *you*
who complained about global variables. Those "to which you
expose update rights over a wide area network". Whatever.

> > Can you come up with a reasonable
> > interpretation of "global variable" that would render
> > the term sufficiently ambiguous to be unusable?
>
> You start - give me *one* definition of "global variable".
>
> <snip>

Yevgen

Richard Heathfield

unread,
Apr 16, 2008, 4:11:19 AM4/16/08
to
ymun...@gmail.com said:

<snip>



> All right, you won. You are right as always.

You haven't demonstrated whether I'm right or wrong. You've simply refused
to say what you're talking about.

> Right amount
> of snipping, and it's no longer obvious that it was *you*
> who complained about global variables.

Of course it's obvious - to anyone who has read the thread. And to anyone
who hasn't, I'll make my point clear right now - what do you MEAN by
global variables?

Why the big secret? If it's such a wonderful term, why won't anyone tell me
what it means?

Richard Bos

unread,
Apr 16, 2008, 4:06:44 AM4/16/08
to
c...@tiac.net (Richard Harter) wrote:

> As a side note, by my lights Twink et al are not trolls - they
> are critics. A troll proper posts rubbish with the object of
> raising controversy.

No, a troll posts to yank cranks. Real controversy is not the aim;
stirring up irritation is.

Richard

Richard Bos

unread,
Apr 16, 2008, 4:10:22 AM4/16/08
to
ric...@cogsci.ed.ac.uk (Richard Tobin) wrote:

> Richard Heathfield <r...@see.sig.invalid> wrote:
>
> >Give a suitable declaration, and I'll show you a part of a C program where
> >this "variable" is not visible. The technique is obvious:
>
> I thought of mentioning that, but I really thought that everyone, even
> in this newsgroup, would take the obvious interpretation.

You'd think wrong; or at least, you'd think wrong if you think that a
first year student asking about global variables would have considered
that there is a difference between file scope identifiers with internal
and those with external linkage; and that in some cases in which one is
concerned about "global objects" this difference is important, and in
others, it isn't.

Richard

Richard Heathfield

unread,
Apr 16, 2008, 4:21:51 AM4/16/08
to
Richard Harter said:

> On 15 Apr 2008 12:42:23 -0700, Keith Thompson <k...@cts.com>

<snip>

>><OT>
>>Richard, I've never thought of you as a troll. Out of curiosity, why
>>have you started calling yourself one?
>></OT>
>
> It's a "poking a little fun at Heathfield" thing, referring to a
> dogmatic passage in one of his posts. I'll be good and stop.

<shrug>

> As a side note, by my lights Twink et al are not trolls - they
> are critics.

I'm not entirely convinced that they[1] qualify either as critics or as
trolls. I've just been re-reading the Jargon File definition, and it
strikes me that, if trolling /is/ their intention, they're not very good
at it. As critics, they do even less well, since they don't actually know
the language well enough to pass judgement on the articles of those who do
(and in any case, they rarely bother even to mention the language).

[1] I am given to understand that "Antoninus Twink" has recently changed
his ways, and has started posting attempts at technical responses. If that
is the case, it is to be welcomed, and probably merits his exclusion from
'they'. Whether it is the case or not, however, it's probably best if he
stays in my killfile, for the sake of a quieter life for both of us.

ymun...@gmail.com

unread,
Apr 16, 2008, 4:23:15 AM4/16/08
to
On Apr 16, 3:11 am, Richard Heathfield <r...@see.sig.invalid> wrote:

> ymunt...@gmail.com said:
>
> <snip>
>
> > All right, you won. You are right as always.
>
> You haven't demonstrated whether I'm right or wrong. You've simply refused
> to say what you're talking about.

Oh yes I did say what I was talking about. About
word games, about someone who invents his terms
and claims they are standard and "finds" quotes
from the standard to back it up, about someone
being a smart ass, and so on. You got the idea
perfectly well, I am sure. And about wide network
and snipping too, by the way.

>
> > Right amount
> > of snipping, and it's no longer obvious that it was *you*
> > who complained about global variables.
>
> Of course it's obvious - to anyone who has read the thread. And to anyone
> who hasn't, I'll make my point clear right now - what do you MEAN by
> global variables?
>
> Why the big secret? If it's such a wonderful term, why won't anyone tell me
> what it means?

Funny. You said this bunch of times, others said this
bunch of times: it's ambiguous, it's not defined. There
were attempts to come up with a strict definition, no
idea if they succeeded or not. So what? No matter how
hard you try, "global variable" won't be related to "wide
network", no. Only to "smart ass".

Yevgen

Richard Heathfield

unread,
Apr 16, 2008, 4:37:06 AM4/16/08
to
ymun...@gmail.com said:

> On Apr 16, 3:11 am, Richard Heathfield <r...@see.sig.invalid> wrote:
>> ymunt...@gmail.com said:
>>
>> <snip>
>>
>> > All right, you won. You are right as always.
>>
>> You haven't demonstrated whether I'm right or wrong. You've simply
>> refused to say what you're talking about.
>
> Oh yes I did say what I was talking about.

I must have missed where you defined "global variable".

> About
> word games, about someone who invents his terms

You mean "global variable"?

> and claims they are standard

Oh, you mean "file scope objects". You don't seem to have any difficulty
understanding what I meant by the term, as you yourself have agreed. I
note, however, that you continue to fail to explain what *you* mean by
"global variable". Why is that?

>> Why the big secret? If it's such a wonderful term, why won't anyone tell
>> me what it means?
>
> Funny. You said this bunch of times, others said this
> bunch of times: it's ambiguous, it's not defined.

Right.

> There
> were attempts to come up with a strict definition, no
> idea if they succeeded or not. So what?

So we can't work out what we mean by "global variable", it seems.

> No matter how
> hard you try, "global variable" won't be related to "wide
> network", no.

How do you know? We don't have a definition for the term yet.

> Only to "smart ass".

I'm still trying to find out what you mean by "global variable", but all
you do is come up with insults and evasions. I am beginning to suspect
that you don't actually know what you mean.

Nick Keighley

unread,
Apr 16, 2008, 4:46:45 AM4/16/08
to
On 16 Apr, 09:21, Richard Heathfield <r...@see.sig.invalid> wrote:
> Richard Harter said:
> > On 15 Apr 2008 12:42:23 -0700, Keith Thompson <k...@cts.com>

> >><OT>


> >>Richard, I've never thought of you as a troll.  Out of curiosity, why
> >>have you started calling yourself one?
> >></OT>
>
> > It's a "poking a little fun at Heathfield" thing, referring to a
> > dogmatic passage in one of his posts.  I'll be good and stop.
>
> <shrug>
>
> > As a side note, by my lights Twink et al are not trolls - they
> > are critics.
>
> I'm not entirely convinced that they[1] qualify either as critics or as
> trolls. I've just been re-reading the Jargon File definition, and it
> strikes me that, if trolling /is/ their intention, they're not very good
> at it. As critics, they do even less well, since they don't actually know
> the language well enough to pass judgement on the articles of those who do
> (and in any case, they rarely bother even to mention the language).

I'm not sure I agree with this, I think even Twink's C knowledge is
fairly good.


> [1] I am given to understand that "Antoninus Twink" has recently changed
> his ways, and has started posting attempts at technical responses.

yes, but off-topic technical responses

> If that
> is the case, it is to be welcomed, and probably merits his exclusion from
> 'they'. Whether it is the case or not, however, it's probably best if he
> stays in my killfile, for the sake of a quieter life for both of us.

given the off-topicness, yes, it probably would be a good idea
if he reamined in your killfile.


--
Nick Keighley


Nick Keighley

unread,
Apr 16, 2008, 4:58:34 AM4/16/08
to
On 15 Apr, 22:47, lawrence.jo...@siemens.com wrote:
> Keith Thompson <k...@cts.com> wrote:

> > You think that the term "global variable" is perfectly clear.  You may
> > well be right.  But if you're not able to provide a definition, then I
> > suggest that you don't really understand yourself what it means.
>
> It's apparently like pornography -- hard to define, but you know it when
> you see it.  :-)

your post has just prevented me from posting a long rambling
response which amounts to what you just said.

"global variable" is not a well defined term, that
doesn't mean it isn't a useful term.

Now "module" I'd argue is even less useful.


--
Nick Keighley

ymun...@gmail.com

unread,
Apr 16, 2008, 5:10:03 AM4/16/08
to
On Apr 16, 3:37 am, Richard Heathfield <r...@see.sig.invalid> wrote:
> ymunt...@gmail.com said:
>
> > On Apr 16, 3:11 am, Richard Heathfield <r...@see.sig.invalid> wrote:
> >> ymunt...@gmail.com said:
>
> >> <snip>
>
> >> > All right, you won. You are right as always.
>
> >> You haven't demonstrated whether I'm right or wrong. You've simply
> >> refused to say what you're talking about.
>
> > Oh yes I did say what I was talking about.
>
> I must have missed where you defined "global variable".

I did not. Did I say I did?

>
> > About
> > word games, about someone who invents his terms
>
> You mean "global variable"?

No, "file scope object".

> > and claims they are standard
>
> Oh, you mean "file scope objects". You don't seem to have any difficulty
> understanding what I meant by the term, as you yourself have agreed.

Yep. But, while we are talking about unambiguous terms,
if you have this declaration outside of any block or list
of parameters:

int a[10];

is a[1] a file scope object? I don't think so, yet I can't
see why one couldn't think so. It's certainly not as stupid
as "wide network" thing. I personally would call stupid
anyone who claimed a[1] was a file scope object here. But
I call stupid anyone who claims that there are no global
variables too, so you may disagree.

> I
> note, however, that you continue to fail to explain what *you* mean by
> "global variable". Why is that?

Because I don't want to (don't pull it out and quote
without the following text, either snip it all or
don't break or cut it). Really, it's pretty hard:
I am not sure how to say that it's a variable which
refers to an object declared in a declaration in which
the identifier has file scope and external linkage
(and this one is where the ambiguity lives). I have no
idea how to define "variable". It's something like:
a declared object. As opposed to elements of arrays
or allocated objects. But I wouldn't really try to
come up with a strict definition. It simply doesn't
make sense: humans (except you) understand what it
means, compiler doesn't care - I got to think about
objects and identifiers when talking to compiler.

> >> Why the big secret? If it's such a wonderful term, why won't anyone tell
> >> me what it means?
>
> > Funny. You said this bunch of times, others said this
> > bunch of times: it's ambiguous, it's not defined.
>
> Right.
>
> > There
> > were attempts to come up with a strict definition, no
> > idea if they succeeded or not. So what?
>
> So we can't work out what we mean by "global variable", it seems.

Yes we can. Given a good will. I can talk to someone,
and I can ask him to clarify what he means in case of
misunderstanding. But only you can pull "wide network"
out of "global variable".

>
> > No matter how
> > hard you try, "global variable" won't be related to "wide
> > network", no.
>
> How do you know? We don't have a definition for the term yet.

How do I know? I don't. How do I know that there
are no aliens in my fridge? I don't. But I tell
you what, there are no aliens in my fridge. And
the OP didn't mean something related to "wide
network". Now, you didn't say a single time (even
though you were asked few times) that you did think
the OP *could* mean anything like that. Of course,
you didn't think so! But, "we don't have a definition".
Yeah.

> > Only to "smart ass".
>
> I'm still trying to find out what you mean by "global variable", but all
> you do is come up with insults and evasions. I am beginning to suspect
> that you don't actually know what you mean.

Come on, this is even cheaper than insults. Let's just
stop it, shall we? I won't call you a smart ass, and
you won't have to get out of your skin in order to
demonstrate how "wide network" is reasonable in a thread
in comp.lang.c about global variables in a C program.
You get the last word and I promise not to read it until
after tomorrow.

Yevgen

Nick Keighley

unread,
Apr 16, 2008, 5:18:02 AM4/16/08
to
On 16 Apr, 07:11, Richard Heathfield <r...@see.sig.invalid> wrote:
> ymunt...@gmail.com said:

> > You snipped the most important part here. Your "proof"
> > and what was wrong with that "proof", namely that you
> > are freely playing with "object" and "scope" and "variable"
> > words, to make it look like the standard itself somehow
> > says there are no global variables.
>
> The Standard does NOT say there are no global variables, and I never
> claimed that it does. The Standard does NOT say that there are no
> leprechauns, either. So what?
>
> >> > "Variable" is not equivalent to "object" even if you say so
>
> >> Okay, what *is* it equivalent to, in the C language? Please justify your
> >> answer with reference to the language definition.
>
> > As far as I know, the C standard does not define "variable".
>
> Right.

<head_position=above_parapet>

Def:
a variable is a binding between an identifier and an area of storage.

Def:
an object is an area of storage

> > It does use that term, and the meaning is, apparently, what
> > "everybody means" by that.
>
> Which is what?
>
> As long as you continue to evade this point, you don't have an argument.
> And the moment you define what you mean, you'll find that some people
> disagree with you, so it turns out that what "everybody means" by that is
> *not* in fact what everybody means by that. Resolving such an impasse is
> the reason we have Standards in the first place.

Def:
global variable is an identifier with an unnecessarily large
visibility


Richard, my definitions may not be water tight but there are things
in the world (and even comp.lang.c) that are not in the standard.
Many programming standards warn against the use of "global variables".
It's a convenient short hand. Isn't asking someone for a definition
from the standard a little counter-productive if they've already
admitted
it isn't in the standard?

--
Nick Keighley

"Anyone attempting to generate random numbers by deterministic
means is, of course, living in a state of sin."
-- John Von Neumann

Richard

unread,
Apr 16, 2008, 5:52:29 AM4/16/08
to
Antoninus Twink <nos...@nospam.invalid> writes:

> On 15 Apr 2008 at 19:48, Willem wrote:
>> Antoninus wrote:
>> > extern int x;
>> >
>> > double foo(void)
>> > {
>> > int *xp = &x;
>> > double x = 3.14;
>> > /* int x is not visible here */
>> > printf("global x=%d: not visible, but still accessible\n", *xp);
>> > return x;
>> > }
>>
>> int *get_x(void)
>> {
>> static int x;
>> return &x;
>> }
>>
>> So, I guess this version of x is a global variable as well then ?
>
> I was debunking Heathfield's word games, not trying to start playing my
> own. Being globally accessible is a necessary but not sufficient
> condition for being a global variable.
>
> But then, we all know what a global variable is, so this entire
> discussion is pointless.
>
> If the pedants really want a definition, how about "a symbol associated
> with a variable that occurs in the table of externally-visible symbols
> in some object file"?


But C doesn't have global variables. How does that work ? .......

(wink)

Richard Heathfield

unread,
Apr 16, 2008, 6:10:19 AM4/16/08
to
ymun...@gmail.com said:

> On Apr 16, 3:37 am, Richard Heathfield <r...@see.sig.invalid> wrote:
>> ymunt...@gmail.com said:
>>
>> > On Apr 16, 3:11 am, Richard Heathfield <r...@see.sig.invalid> wrote:
>> >> ymunt...@gmail.com said:
>>
>> >> <snip>
>>
>> >> > All right, you won. You are right as always.
>>
>> >> You haven't demonstrated whether I'm right or wrong. You've simply
>> >> refused to say what you're talking about.
>>
>> > Oh yes I did say what I was talking about.
>>
>> I must have missed where you defined "global variable".
>
> I did not. Did I say I did?

No, but I was hoping to get some kind of sense out of this discussion. May
I take it to mean that you don't know what a global variable is? If so,
you're in fine company - because it seems that nobody else does, either.

>> > About
>> > word games, about someone who invents his terms
>>
>> You mean "global variable"?
>
> No, "file scope object".

Ah, I see. So invented terms are okay by you *provided* that nobody knows
what they mean?

>> > and claims they are standard
>>
>> Oh, you mean "file scope objects". You don't seem to have any difficulty
>> understanding what I meant by the term, as you yourself have agreed.
>
> Yep. But, while we are talking about unambiguous terms,
> if you have this declaration outside of any block or list
> of parameters:
>
> int a[10];
>
> is a[1] a file scope object?

> I don't think so, yet I can't see why one couldn't think so.

Well, that is at least a reasonable point at last. In formal terms:

a is an identifier at file scope;
int a[10]; is a definition of an object, an array of 10 ints, that is
identified by the identifier 'a', which has file scope;
a[1] is a member of that array object, and therefore has the same
visibility as that array object. And scope is all about visibility. ("An
identifier is visible (i.e., can be used) only within a region of program
text called its scope", as the Standard says.")

Thus, in a context where it is reasonable to call a[] a file scope object,
it is also reasonable to call a[1] a file scope object. Nevertheless, you
are right to point at that it's merely a shorthand for "object identified
by an identifier at file scope".

> It's certainly not as stupid
> as "wide network" thing. I personally would call stupid
> anyone who claimed a[1] was a file scope object here.

So you think I'm stupid. Fine. Have you worked out what a global variable
is?

> But
> I call stupid anyone who claims that there are no global
> variables too, so you may disagree.

I don't know, because nobody will tell me what a global variable *is*.

>> I
>> note, however, that you continue to fail to explain what *you* mean by
>> "global variable". Why is that?
>
> Because I don't want to

That's obvious.

> (don't pull it out and quote
> without the following text, either snip it all or
> don't break or cut it).

How and what I quote is up to me, as long as I don't change your meaning by
deliberately selective quotation.

> Really, it's pretty hard:
> I am not sure how to say that it's a variable which
> refers to an object declared in a declaration in which
> the identifier has file scope and external linkage
> (and this one is where the ambiguity lives).

What is a "variable which refers to an object"? Or rather, can you give me
an example of a variable that does not refer to an object? Is it your
claim that an identifier at file scope but with internal linkage does
*not* identify a global variable? If so, then you are at odds with some
people's usage of that term.

> I have no idea how to define "variable".

Right - and neither, it seems, does anyone else in comp.lang.c - at least,
not in a way that everyone agrees with.

> It's something like:
> a declared object. As opposed to elements of arrays
> or allocated objects.

So in the following code fragment (and it is only a fragment):

{
int i = 6;
int j[1] = 6;
int *p = &i;
int *q = &j[0];
int *r = malloc(sizeof *r);
if(r != NULL)
/* ... more code goes here ... */
}

p points to a variable, but q does not? Is that what you're saying? Why
isn't j[0] a variable? Can it not vary? Is it const? And while we're at
it, why isn't r[0] a variable?


> But I wouldn't really try to
> come up with a strict definition. It simply doesn't
> make sense:

My point exactly.


> humans (except you) understand what it means,

You can't even demonstrate that *you* understand what it means, let alone
anyone else.

> compiler doesn't care - I got to think about
> objects and identifiers when talking to compiler.

Precisely. And we know what we mean by "object" and "identifier" - or at
least, if we don't, we can be corrected by recourse to an acknowledged
authority, the C Standard. I see that you're coming round to my point of
view after all.

<snip>

>> So we can't work out what we mean by "global variable", it seems.
>
> Yes we can. Given a good will.

I sense very little good will in your responses to me.

> I can talk to someone,
> and I can ask him to clarify what he means in case of
> misunderstanding. But only you can pull "wide network"
> out of "global variable".

In other words, I can pull "we don't have a definition for this term" out
of a term that we don't have a definition for. Yes, that's right. Well
done.

>> > No matter how
>> > hard you try, "global variable" won't be related to "wide
>> > network", no.
>>
>> How do you know? We don't have a definition for the term yet.
>
> How do I know? I don't.

Right. So let's start off with *my* reasonable working definition for
"global variable", using the dictionary as my guide - and you tell me why
you think it's wrong, okay?

So, here are Chambers Dictionary's definitions of "global" and "variable"
in their computing senses (/comput./, as Chambers has it):

"global: involving a whole file of data"
"variable: an expression which may have any of a number of values"

So we deduce that a global variable is an expression which may have any of
a number of values, involving a whole file of data.

I'm struggling to come up with a C example, although I suppose an array
into which a datafile has just been read in its entirety might conceivably
qualify - and that could be an *auto* array!

> How do I know that there
> are no aliens in my fridge? I don't.

Right.

> But I tell
> you what, there are no aliens in my fridge.

Look more carefully. They hide behind the butter.

> And
> the OP didn't mean something related to "wide
> network".

Perhaps he didn't. Or perhaps he did. Presumably he knows what he meant by
"global variable", which you can't even define. Are you claiming to be a
mind-reader?

> You get the last word

Like I care. What I *do* care about is that the term "global variable" is
NOT USEFUL, because it is not well-defined. That's why I'm making a big
deal about it. It's nothing to do with your irrational dislike for my
writing style, and everything to do with discussing C in such a way that
everyone knows what is meant by what is being said, so that they can
measure the discussion against the Standard.

Richard Heathfield

unread,
Apr 16, 2008, 6:31:42 AM4/16/08
to
Nick Keighley said:

<snip>

> <head_position=above_parapet>

Don't panic, Nick - I'm not shooting.

>
> Def:
> a variable is a binding between an identifier and an area of storage.

That's a curious definition. Presumably you mean (and I know ICBW about
this!) that a variable /associates/ an identifier with an area of storage.

Clearly it is possible for an identifier to be associated with more than
one area of storage (trivial example: recursive function), so presumably
we can agree that identifiers and variables are not the same thing. But,
as Yevgen Muntyan has so rightly pointed out, scope is to do with
identifiers, not objects, and "global" is surely to do with scope, is it
not?

> Def:
> an object is an area of storage

Yes, ISO 9899 defines it.

<snip>



> Def:
> global variable is an identifier with an unnecessarily large
> visibility

From what you said above, though, a global variable isn't an identifier!
And it can't have visibility, because it is identifiers that have
visibility.

> Richard, my definitions may not be water tight

Well, no, I don't think they are, but at least you had a go.

> but there are things
> in the world (and even comp.lang.c) that are not in the standard.

Sure - but the test is: "are they useful?" I am not convinced that the term
"global variable" is sufficiently useful to overcome the fact that nobody
can tell us what one is.

> Many programming standards warn against the use of "global variables".
> It's a convenient short hand.

Shorthand for what? :-)

> Isn't asking someone for a definition
> from the standard a little counter-productive if they've already
> admitted it isn't in the standard?

At the very least, it ought to be possible - if the term is a useful one -
to come up with a definition for it that nobody objects to, and which
doesn't actually conflict with the Standard!

Chris Dollin

unread,
Apr 16, 2008, 7:30:03 AM4/16/08
to
Richard Heathfield wrote:

> Clearly it is possible for an identifier to be associated with more than
> one area of storage (trivial example: recursive function), so presumably
> we can agree that identifiers and variables are not the same thing. But,
> as Yevgen Muntyan has so rightly pointed out, scope is to do with
> identifiers, not objects, and "global" is surely to do with scope, is it
> not?

Not /just/ scope; there's an implication that the variable has
unbounded [ie until the program terminates] extent. (Hence the
Poplog term `permanent variable`, for example, precisely to
avoid the confusion of "global", especially since it uses that
word for something else again ...)

> Sure - but the test is: "are they useful?" I am not convinced that the term
> "global variable" is sufficiently useful to overcome the fact that nobody
> can tell us what one is.

I disagree here. "global variable", meaning one that can be accessed
by name & updated by any program component without the definer of the
name having any say in the matter [1], is a useful term because -

>> Many programming standards warn against the use of "global variables".

- it identifies a common form of coupling between components.

>> It's a convenient short hand.
>
> Shorthand for what? :-)

See above.

>> Isn't asking someone for a definition
>> from the standard a little counter-productive if they've already
>> admitted it isn't in the standard?
>
> At the very least, it ought to be possible - if the term is a useful one -
> to come up with a definition for it that nobody objects to,

Optimist. There aren't enough short phrases available to cover the
space of useful distinctions; /someone/ is always going to complain
that their example doesn't fit the Procrustean bed of a specific
definition.

Useful terms don't always have to be nailed down in a globally
exact way.

[1] So public static members of C++ or Java classes count as
global variables, and protected members arguably so.
C variables with external linkage count as globals
in this sense, since any other compilation unit can
contain an `extern Wosstype Wossname` and code to
dance all over `Wossname`.

A file-scope non-extern-linkage variable isn't global
by this definition, since no other compilation units
can see it, but the Bad Coupling global variables allow
can still occur within a single unit.

C's freewheeling attitude to pointers of course makes
for exciting and worthwhile nitpicking, since in

static int notGlobalHonestGuv = 17;
int *global = &notGlobalHonestGuv;

the /visibility/ of nghg doesn't make any real difference
to its global accessibility: claims that it's not a
global variable should be treated with suspicion.

A hole in the scope of a variable need not deglobalise
it. (When the "components" I've handwaved are compilation
units, you can introduce a hole round them anyway.)

--
"Oh, but it can be applied to almost any situation." /Tactics of Mistake/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Richard Heathfield

unread,
Apr 16, 2008, 7:54:28 AM4/16/08
to
Chris Dollin said:

> Richard Heathfield wrote:
>
>> Clearly it is possible for an identifier to be associated with more than
>> one area of storage (trivial example: recursive function), so presumably
>> we can agree that identifiers and variables are not the same thing. But,
>> as Yevgen Muntyan has so rightly pointed out, scope is to do with
>> identifiers, not objects, and "global" is surely to do with scope, is it
>> not?
>
> Not /just/ scope; there's an implication that the variable has
> unbounded [ie until the program terminates] extent.

Okay, so that rules out auto - according to you, at any rate.

> (Hence the
> Poplog term `permanent variable`, for example, precisely to
> avoid the confusion of "global", especially since it uses that
> word for something else again ...)

This in itself suggests that what is meant by "global" varies according to
the context - i.e. the semantics of "global" is local.


>> Sure - but the test is: "are they useful?" I am not convinced that the
>> term "global variable" is sufficiently useful to overcome the fact that
>> nobody can tell us what one is.
>
> I disagree here. "global variable", meaning one that can be accessed
> by name & updated by any program component without the definer of the
> name having any say in the matter [1],

This rules out static, which leaves extern (objects identified by an
identifier at file scope and with external linkage).

And can we all agree on that? Are there any dissenters?

And when someone describes as "global" an identifier at file scope but with
*internal* linkage, is it okay to tell him he's wrong? If not, why not?

James Kuyper

unread,
Apr 16, 2008, 7:59:08 AM4/16/08
to
Nick Keighley wrote:
...

> Def:
> a variable is a binding between an identifier and an area of storage.

That doesn't fit conventional usage of the term. Consider a typical
usage: "This variable has a value of 3." While the binding is highly
relevant to the definition of the term 'variable', it is not what that
term directly refers to. Neither does it refer to the identifier that is
bound. The term variable refers to the object that is bound to the
identifier.

In much simpler terms: a variable is a named object.

...


> Def:
> global variable is an identifier with an unnecessarily large
> visibility

That also does not fit conventional usage. You've taken a value judgment
about global variables, and converted that judgment into the definition.
Consider:

char* aaah(int n, char s[])
{
int i;
for(i=0; i<n; i++)
s[i] = 'a';

return s+n;
}

According to your definition, 'i' is a global variable, because it is
visible at the return statement, which is not a place where it is
necessary for it to be visible. The function could have been re-written
to use:

for(int i=0; i<n; i++)

Personally, I like this C99 feature, and favor its use in any code that
is not required to be C90-compatible. However, that's a value judgment,
and shouldn't have anything to do with the definition of 'global variable'.

On the flip side, your definition could arguably be used to deny the
label 'global variable' to a variable with file scope and external
linkage, if the global visibility of that variable is in fact necessary.
Obviously, if you insist on the dogma that it can never be justified, no
such possibility exists. While I think that global variables are almost
always design errors, I insist on the word "almost" - there are exceptions.

In any event, the definition of whether or not something is a global
variable should not depend upon individual value judgments about the
merits of global visibility.

My own definition of 'global variable' in a C context is

"a variable whose identifier has file scope and external linkage"

I don't think file scope gives a variable sufficient visibility to
justify the the adjective 'global'. I don't think the fact that such
variables can be hidden by a local declaration is sufficient
justification to deny them that label.

As I've defined them, C has variables and in particular global
variables, even though the C standard itself provides no definitions for
either of those terms. That's no different from the fact that C has a
grammar, even though the C standard never defines the term 'grammar'.
The C standard doesn't have to be the source of the definition of every
term that it is useful to use while describing C.

Richard Bos

unread,
Apr 16, 2008, 8:05:10 AM4/16/08
to
James Kuyper <james...@verizon.net> wrote:

> My own definition of 'global variable' in a C context is
>
> "a variable whose identifier has file scope and external linkage"
>
> I don't think file scope gives a variable sufficient visibility to
> justify the the adjective 'global'.

A common beginner's question is: "Why does everybody say that global
objects are bad for recursive functions?" External linkage is entirely
irrelevant to that question. And that's why it is best to specify what
you mean when you ask about "global functions". In some cases, linkage
is important; in others, it isn't.

Richard

Richard Tobin

unread,
Apr 16, 2008, 8:50:25 AM4/16/08
to
In article <49aa56ea-7b5d-40a8...@u69g2000hse.googlegroups.com>,
<istil...@gmail.com> wrote:

>I have several further questions on global variables.

>When are they used? I.e., in what situations it is good to use them.

It's obvious what's *good* about global variables: they're useful
whenever you want to use the same variable in lots of different parts
of the program.

The trouble is that you sometimes find that you want to take a program
and modify it or re-use it in a way that doesn't work with global
variables. For example, suppose you write a program to read a file,
and you use a global variable to store the FILE * that you read with
(quite natural because you use it in lots of places). Now you find
you want to read two files at once (one line from each at a time,
perhaps). You have to change the program to pass the FILE * around
as an argument.

So when should you use a global variable? When the simplicity and
convenience outweigh the difficulty of re-using the code. That's
not a technical question - it depends on your circumstances.

-- Richard
--
:wq

Chris Dollin

unread,
Apr 16, 2008, 8:52:11 AM4/16/08
to
Richard Heathfield wrote:

> Chris Dollin said:
>
>> Richard Heathfield wrote:
>>
>>> Clearly it is possible for an identifier to be associated with more than
>>> one area of storage (trivial example: recursive function), so presumably
>>> we can agree that identifiers and variables are not the same thing. But,
>>> as Yevgen Muntyan has so rightly pointed out, scope is to do with
>>> identifiers, not objects, and "global" is surely to do with scope, is it
>>> not?
>>
>> Not /just/ scope; there's an implication that the variable has
>> unbounded [ie until the program terminates] extent.
>
> Okay, so that rules out auto - according to you, at any rate.

I wouldn't describe any C automatic variable as a "global variable"
without further qualification ("`x` is global to the entire 800
lines of the smudgereification function").

>> (Hence the
>> Poplog term `permanent variable`, for example, precisely to
>> avoid the confusion of "global", especially since it uses that
>> word for something else again ...)
>
> This in itself suggests that what is meant by "global" varies according to
> the context - i.e. the semantics of "global" is local.

Parametrised, with the default being globally global.

>>> Sure - but the test is: "are they useful?" I am not convinced that the
>>> term "global variable" is sufficiently useful to overcome the fact that
>>> nobody can tell us what one is.
>>
>> I disagree here. "global variable", meaning one that can be accessed
>> by name & updated by any program component without the definer of the
>> name having any say in the matter [1],
>
> This rules out static, which leaves extern (objects identified by an
> identifier at file scope and with external linkage).

I covered this in the footnote.

> And can we all agree on that? Are there any dissenters?

Is water wet? Do Eddorians crave power?

> And when someone describes as "global" an identifier at file scope but with
> *internal* linkage, is it okay to tell him he's wrong?

That depends. The variable is global to the file, but not (necessarily)
to the program. Which did they mean?

Communication beats definitional cat-vacuuming.

--
"Thereafter, events may roll unheeded." /Foundation/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Richard Heathfield

unread,
Apr 16, 2008, 9:05:40 AM4/16/08
to
Chris Dollin said:

> Richard Heathfield wrote:
>
>> Chris Dollin said:
>>
<snip>


>>>
>>> Not /just/ scope; there's an implication that the variable has
>>> unbounded [ie until the program terminates] extent.
>>
>> Okay, so that rules out auto - according to you, at any rate.
>
> I wouldn't describe any C automatic variable as a "global variable"
> without further qualification ("`x` is global to the entire 800
> lines of the smudgereification function").

Well, it was a sideways ref to my attempt to cobble together a
dictionary-based definition, which used mainstream computer-related
interpretations of "global" and "variable" and which *didn't* rule out
auto.

>>> (Hence the
>>> Poplog term `permanent variable`, for example, precisely to
>>> avoid the confusion of "global", especially since it uses that
>>> word for something else again ...)
>>
>> This in itself suggests that what is meant by "global" varies according
>> to the context - i.e. the semantics of "global" is local.
>
> Parametrised, with the default being globally global.

For a parameter, even a terminology parameter, to make sense locally, its
semantics must be defined locally (even if that definition consists of a
reference to an external definition).

<snip>

>> And when someone describes as "global" an identifier at file scope but
>> with *internal* linkage, is it okay to tell him he's wrong?
>
> That depends. The variable is global to the file, but not (necessarily)
> to the program. Which did they mean?

I have no idea, and I find it tedious to ask them, because they all seem to
mean something different.

> Communication beats definitional cat-vacuuming.

Well, at least it's not as loud.

James Kuyper

unread,
Apr 16, 2008, 9:24:33 AM4/16/08
to

Well, as far as that goes, file scope is also irrelevant to that
question. Static storage duration is what's really relevant. Use of such
a variable is just as problematic in recursive functions whether it has
static storage duration because it's defined at file scope or because
it's explicitly declared with the 'static' keyword with block scope.

Therefore, "global variable" is the wrong term to use for that question.
There would be some value in defining a short phrase that means
"variable with static storage duration". The term "static variable" is
clearly unsuitable because it's ambiguous as to which meaning of
"static" it refers to. However, "global variable" is equally clearly not
the right short form for "variable with static storage duration".

I would use "file scope variables" in any context where file scope is
relevant, but external linkage is not. I would only use the term "global
variables" when both factors are relevant.

In fact, now that I think about it, the key issue is simply the fact
that the variable has external linkage. If it has such linkage, and is
defined in C code, then it necessarily has file scope in the C code
which defines it. However, an object with external linkage presents
precisely the same issues whether it was defined in C or in some other
language, even if the only declaration of it in C code has block scope:

int get_turn(void)
{ /* Silly example, just to clarify what I am referring to */
extern int turn_number;
return turn_number;
}

Therefore, I will simplify my definition. I use the phrase "global
variable" exclusively as a short form for "variable with external linkage".

Richard Tobin

unread,
Apr 16, 2008, 11:08:59 AM4/16/08
to
In article <l6nNj.9225$XF3.2026@trnddc04>,
James Kuyper <james...@verizon.net> wrote:

>Therefore, "global variable" is the wrong term to use for that question.
>There would be some value in defining a short phrase that means
>"variable with static storage duration". The term "static variable" is
>clearly unsuitable because it's ambiguous as to which meaning of
>"static" it refers to. However, "global variable" is equally clearly not
>the right short form for "variable with static storage duration".

Surely "non-local" is the obvious term in this case. Problems with
variables in recursive functions are likely to hinge on whether of not
the variable is local to the function being recursively called;
exactly what its scope is if it's not local is unimportant.

>Therefore, I will simplify my definition. I use the phrase "global
>variable" exclusively as a short form for "variable with external linkage".

Yes, or if you want to get closer to the C standard's terminology
"object identifier with external linkage". But that is not the
definition of global variable, because the term is not exclusive to C.
It's just what global variables turn out to be in C, just as "strings"
turn out to be arrays of characters terminated by '\0'.

-- Richard
--
:wq

Joe Wright

unread,
Apr 16, 2008, 11:12:13 AM4/16/08
to
ymun...@gmail.com wrote:
> On Apr 14, 11:54 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
>> Ben Bacarisse said:
>>
>>
>>
>>> Richard <de...@gmail.com> writes:
>>>> lawrence.jo...@siemens.com writes:
>>>>> Richard <de...@gmail.com> wrote:
>>>>>> It should be apparent to an sentient being what a "variable" is in a
>>>>>> programming newsgroup. Even this one. Ditto for "global".
>>>>> "Variable" shouldn't be controversial, even the C Standard uses the
>>>>> term. But "global variable" is a slipperier concept in C. For
>>>>> example,
>>>>> does a file scope static variable qualify or not? It's not exactly a
>>>> No. Because its file scope. Not global scope. I don think I'm being
>>>> overly simplistic here.
>>> You (correctly in my opinion) exclude it, but you should exclude it
>>> because it does not have external linkage.
>> Excluding it because it doesn't have global scope is perfectly correct.
>> "There are four kinds of scopes: function, file, block, and function
>> prototype", quoth the Standard. Since no object can be considered global,
>> even by a troll, unless it has global scope, and since there is no such
>> thing as global scope in C, *therefore* C doesn't have global objects (or
>> "variables", as some people like to call them).
>
> What scope do allocated objects have? Are you sure you
> are not confusing objects and identifiers? "Variable"
> is not equivalent to "object" even if you say so ("some
> people...", yeah), you're just playing stupid word games
> here.
>
Either you don't have the right books or you don't read enough. Please
refer to K&R2 Section A4. Meaning of Identifiers.

The single paragraph says an identifier is a name used to refer to any
of several things including objects. "An object, sometimes called a
variable, is a location in storage and its interpretation depends on two
main attributes: its storage class and its type." At least Brian
Kernighan thinks object and variable are interchangeable. "A name also
has scope, which is the region of the program in which it is known, and
linkage, which determines whether the same name in another scope refers
to the same object or function."

> The only argument you can present is: the standard doesn't
> define "global variables". Many people will disagree that
> this argument is worth a shit, even you are trying to back
> it up by some nonsense. Nevertheless, it's a valid argument
> and one may say that in technical discussions it's important
> not to use ambiguous terms, yadda yadda. Is it what you're
> saying? Doesn't look so, more it looks like someone is
> trying to be a smart ass without doing his homework. Or
> do you really think that "global variable" term is invented
> by morons?
>
No, not by morons, but people who cannot be precise in what they mean by
the term. We concede "global variable" is not defined in the C Standard
but I know what it means to me. Consider a program consisting of two
translation units, foo.c and bar.c where we would define a variable in
foo which would be visible from bar. Something like this..

foo.c
#include <stdio.h>
int glob = 42; /* External Linkage */
int main(void) {
printf("%d\n", bar());
return 0;
}

bar.c
extern int glob; /* Refers to the External glob in foo.c */
int bar(void) {
return glob;
}

That's what I mean when I say "global variable". What do you mean when
you say it?

> Yevgen


--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

Richard Tobin

unread,
Apr 16, 2008, 11:42:31 AM4/16/08
to
In article <gNSdnRYJHJ7QipvV...@comcast.com>,
Joe Wright <joeww...@comcast.net> wrote:

>The single paragraph says an identifier is a name used to refer to any
>of several things including objects. "An object, sometimes called a
>variable, is a location in storage and its interpretation depends on two
>main attributes: its storage class and its type." At least Brian
>Kernighan thinks object and variable are interchangeable.

Human language has subtleties that are not always amenable to
precise definitions.

When you do

int *x = malloc(10 * sizeof(x));

you would probably agree that you are allocating an array of 10
objects. But would you say that you are allocating an array of
10 variables?

I wouldn't think of, say, the third int in the array as a variable.
But I might well call x[2] a variable. If f() returned x I would be
slightly less inclined to call f(x)[2] a variable. All quite
illogical in a sense, but that's the way it is. I call objects
variables when I think of them as named. And the more complicated the
expression, the less name-like it is.

I just checked with a very exerienced colleague, and he thinks
that "x" is a variable but "x[2]" isn't. So between him and K&R,
I think my position is a sensible, moderate one :-)

-- Richard
--
:wq

Joe Wright

unread,
Apr 16, 2008, 11:57:10 AM4/16/08
to
istil...@gmail.com wrote:
> On Apr 12, 1:16 am, Ian Collins <ian-n...@hotmail.com> wrote:
> If you simply want to remove the global, make silent static
>> and expose it through a getSilent() function.
>
> Following your suggestion, I wrote
>
> main.c
>
> static int silent; /* global in main.c */
> int getSilent()
> {
> return silent;
> }
>
> int main(int argc, char *argv[])
> {
> if (some condition)
> silent = 1;
> }
>
> ----
>
> other.c
>
> How to call getSilent()? The compiler would warn "undefined
> getSilent()".
>

/* foo.c looks like this..*/
#include <stdio.h>

int bar(void);

static int silent = 42;

int getsilent(void) {
return silent;
}

int main(void) {
printf("%d\n", silent);


printf("%d\n", bar());
return 0;
}

/* bar.c looks like this..*/
int getsilent(void);

int bar(void) {
return getsilent() + 2;
}

Works here..

James Kuyper

unread,
Apr 16, 2008, 12:25:39 PM4/16/08
to
Richard Tobin wrote:
> In article <l6nNj.9225$XF3.2026@trnddc04>,
> James Kuyper <james...@verizon.net> wrote:
>
>> Therefore, "global variable" is the wrong term to use for that question.
>> There would be some value in defining a short phrase that means
>> "variable with static storage duration". The term "static variable" is
>> clearly unsuitable because it's ambiguous as to which meaning of
>> "static" it refers to. However, "global variable" is equally clearly not
>> the right short form for "variable with static storage duration".
>
> Surely "non-local" is the obvious term in this case. Problems with
> variables in recursive functions are likely to hinge on whether of not
> the variable is local to the function being recursively called;
> exactly what its scope is if it's not local is unimportant.

Well, that's a different kind of problem than the one I was thinking of;
but non-local is certainly the right way to describe the variables it
applies to.

>> Therefore, I will simplify my definition. I use the phrase "global
>> variable" exclusively as a short form for "variable with external linkage".
>
> Yes, or if you want to get closer to the C standard's terminology
> "object identifier with external linkage". But that is not the

Not quite. As I understand the term, it refers to the identified object,
not to the identifier of the object. What I'm talking about could be
described as an "object with an externally linked identifier".

> definition of global variable, because the term is not exclusive to C.
> It's just what global variables turn out to be in C, just as "strings"
> turn out to be arrays of characters terminated by '\0'.

If you want "global variable" to be a language-independent term, that's
going to be a lot harder to define in a way that can be usefully applied
to the C language. The simplest language-independent definition I can
think of, "variable which is guaranteed to be visible from anywhere in a
program", has no C referent. That is because, as Richard Heathfield has
already pointed out, any variable can be rendered invisible in certain
parts of the program, by declaration of a variable with a different
scope and the same name. Such a variable isn't even visible in a given
translation unit if until it has been declared, and that declaration
could have block scope instead of file scope. I don't know of any simple
modification of that definition will allow it to refer to anything which
actually exists in C, without losing language independence.

I think it's much more useful to acknowledge that there's little that
can meaningfully be said about global variables that is language
independent, and that it's more useful to let the definition be language
specific.

Keith Thompson

unread,
Apr 16, 2008, 12:59:38 PM4/16/08
to
Richard Heathfield <r...@see.sig.invalid> writes:
[...]

> [1] I am given to understand that "Antoninus Twink" has recently changed
> his ways, and has started posting attempts at technical responses. If that
> is the case, it is to be welcomed, and probably merits his exclusion from
> 'they'. Whether it is the case or not, however, it's probably best if he
> stays in my killfile, for the sake of a quieter life for both of us.

He's started posting technical responses, but they've been uniformly
off-topic. It's mostly stuff that would probably be perfectly
appropriate in comp.unix.programmer. He's trying to encourage
system-specific discussions here in comp.lang.c. I believe this is
just a more subtle form of trolling. I'm at least mildly concerned
that it might actually work and cause some real damage to the
newsgroup.

--
Keith Thompson (The_Other_Keith) <ks...@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Richard

unread,
Apr 16, 2008, 1:58:35 PM4/16/08
to
James Kuyper <james...@verizon.net> writes:

> Richard Tobin wrote:
>> In article <l6nNj.9225$XF3.2026@trnddc04>,
>> James Kuyper <james...@verizon.net> wrote:
>>
>>> Therefore, "global variable" is the wrong term to use for that
>>> question. There would be some value in defining a short phrase that
>>> means "variable with static storage duration". The term "static
>>> variable" is clearly unsuitable because it's ambiguous as to which
>>> meaning of "static" it refers to. However, "global variable" is
>>> equally clearly not the right short form for "variable with static
>>> storage duration".
>>
>> Surely "non-local" is the obvious term in this case. Problems with
>> variables in recursive functions are likely to hinge on whether of not
>> the variable is local to the function being recursively called;
>> exactly what its scope is if it's not local is unimportant.
>
> Well, that's a different kind of problem than the one I was thinking
> of; but non-local is certainly the right way to describe the variables
> it applies to.

Like "non black" to describe white people? What a load of nonsense.

>
>>> Therefore, I will simplify my definition. I use the phrase "global
>>> variable" exclusively as a short form for "variable with external
>>> linkage".
>>
>> Yes, or if you want to get closer to the C standard's terminology
>> "object identifier with external linkage". But that is not the
>
> Not quite. As I understand the term, it refers to the identified
> object, not to the identifier of the object. What I'm talking about
> could be described as an "object with an externally linked
> identifier".
>
>> definition of global variable, because the term is not exclusive to C.
>> It's just what global variables turn out to be in C, just as "strings"
>> turn out to be arrays of characters terminated by '\0'.
>
> If you want "global variable" to be a language-independent term,
> that's going to be a lot harder to define in a way that can be
> usefully applied to the C language. The simplest language-independent


Nonsense. declare "int i" outside of function scope. It's a global. To
give ACCESS to that global is another matter. The you provide the extern
OR, shock horror, some sort of registry access function which can return
a pointer to that global.


> definition I can think of, "variable which is guaranteed to be visible
> from anywhere in a program", has no C referent. That is because, as
> Richard Heathfield has already pointed out, any variable can be
> rendered invisible in certain parts of the program, by declaration of

So what? There is a coffee cup on my table. The fact that I chose to put
a pile of books in front of it doesn't make it any the less there.

> a variable with a different scope and the same name. Such a variable
> isn't even visible in a given translation unit if until it has been
> declared, and that declaration could have block scope instead of file

So? It's still globally accessible where global mean "any other pleace
in the process/program" where access to that global is granted. It is
not "local" to any single function.

> scope. I don't know of any simple modification of that definition will
> allow it to refer to anything which actually exists in C, without
> losing language independence.

I dont know you would want to have such.

>
> I think it's much more useful to acknowledge that there's little that
> can meaningfully be said about global variables that is language
> independent, and that it's more useful to let the definition be
> language specific.

I think its more useful to use the generally accepted term "global" and
not try to be too clever.


Flash Gordon

unread,
Apr 16, 2008, 2:45:06 PM4/16/08
to
Keith Thompson wrote, On 16/04/08 17:59:

> Richard Heathfield <r...@see.sig.invalid> writes:
> [...]
>> [1] I am given to understand that "Antoninus Twink" has recently changed
>> his ways, and has started posting attempts at technical responses. If that
>> is the case, it is to be welcomed, and probably merits his exclusion from
>> 'they'. Whether it is the case or not, however, it's probably best if he
>> stays in my killfile, for the sake of a quieter life for both of us.
>
> He's started posting technical responses, but they've been uniformly
> off-topic. It's mostly stuff that would probably be perfectly
> appropriate in comp.unix.programmer. He's trying to encourage
> system-specific discussions here in comp.lang.c. I believe this is
> just a more subtle form of trolling. I'm at least mildly concerned
> that it might actually work and cause some real damage to the
> newsgroup.

He has also on at least one occasion carefully snipped a post
redirecting someone to comp.unix.programmer so that it appeared to be a
request to post highly system specific code here instead.
--
Flash Gordon

Flash Gordon

unread,
Apr 16, 2008, 2:56:46 PM4/16/08
to
Richard wrote, On 16/04/08 18:58:

> James Kuyper <james...@verizon.net> writes:
>
>> Richard Tobin wrote:
>>> In article <l6nNj.9225$XF3.2026@trnddc04>,
>>> James Kuyper <james...@verizon.net> wrote:
>>>
>>>> Therefore, "global variable" is the wrong term to use for that
>>>> question. There would be some value in defining a short phrase that
>>>> means "variable with static storage duration". The term "static
>>>> variable" is clearly unsuitable because it's ambiguous as to which
>>>> meaning of "static" it refers to. However, "global variable" is
>>>> equally clearly not the right short form for "variable with static
>>>> storage duration".
>>> Surely "non-local" is the obvious term in this case. Problems with
>>> variables in recursive functions are likely to hinge on whether of not
>>> the variable is local to the function being recursively called;
>>> exactly what its scope is if it's not local is unimportant.
>> Well, that's a different kind of problem than the one I was thinking
>> of; but non-local is certainly the right way to describe the variables
>> it applies to.
>
> Like "non black" to describe white people? What a load of nonsense.

No, rather as you could use "non black" to describe someone who could be
white, yellow, green or in fact anything other than white.

>>>> Therefore, I will simplify my definition. I use the phrase "global
>>>> variable" exclusively as a short form for "variable with external
>>>> linkage".
>>> Yes, or if you want to get closer to the C standard's terminology
>>> "object identifier with external linkage". But that is not the
>> Not quite. As I understand the term, it refers to the identified
>> object, not to the identifier of the object. What I'm talking about
>> could be described as an "object with an externally linked
>> identifier".
>>
>>> definition of global variable, because the term is not exclusive to C.
>>> It's just what global variables turn out to be in C, just as "strings"
>>> turn out to be arrays of characters terminated by '\0'.
>> If you want "global variable" to be a language-independent term,
>> that's going to be a lot harder to define in a way that can be
>> usefully applied to the C language. The simplest language-independent
>
> Nonsense. declare "int i" outside of function scope. It's a global. To
> give ACCESS to that global is another matter. The you provide the extern
> OR, shock horror, some sort of registry access function which can return
> a pointer to that global.

Personally I would not consider a file scope object that does not have
external linkage to be a global. In another language I might call it a
"module local variable" or something similar since it is local to that
specific module.

I would say something more like, "a global variable is one that is
visible by name everywhere that it is not shadowed by another variable"
where a variable is approximately, "a named area of storage". These
definition would need tightening up a bit though.

<snip>

> I think its more useful to use the generally accepted term "global" and
> not try to be too clever.

Based on the above your usage of the term "global" is not the same as
mine and I've met others who would agree with my usage.

It is loading more messages.
0 new messages