473,473 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to eliminate this global variable, silent?

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?
Jun 27 '08
112 5332
ym******@gmail.com said:
On Apr 16, 12:24 am, Richard Heathfield <r...@see.sig.invalidwrote:
>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 <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
Jun 27 '08 #51
ym******@gmail.com said:
On Apr 16, 12:16 am, Richard Heathfield <r...@see.sig.invalidwrote:
>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?

--
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
Jun 27 '08 #52
On Apr 16, 1:11 am, Richard Heathfield <r...@see.sig.invalidwrote:
ymunt...@gmail.com said:
On Apr 16, 12:24 am, Richard Heathfield <r...@see.sig.invalidwrote:
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
Jun 27 '08 #53
On Apr 16, 1:21 am, Richard Heathfield <r...@see.sig.invalidwrote:
ymunt...@gmail.com said:
On Apr 16, 12:16 am, Richard Heathfield <r...@see.sig.invalidwrote:
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
Jun 27 '08 #54
ym******@gmail.com said:
On Apr 16, 1:11 am, Richard Heathfield <r...@see.sig.invalidwrote:
<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 <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
Jun 27 '08 #55
ym******@gmail.com said:
On Apr 16, 1:21 am, Richard Heathfield <r...@see.sig.invalidwrote:
<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>

--
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
Jun 27 '08 #56
On Apr 16, 2:59 am, Richard Heathfield <r...@see.sig.invalidwrote:
ymunt...@gmail.com said:
On Apr 16, 1:21 am, Richard Heathfield <r...@see.sig.invalidwrote:

<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
Jun 27 '08 #57
ym******@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 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
Jun 27 '08 #58
cr*@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
Jun 27 '08 #59
ri*****@cogsci.ed.ac.uk (Richard Tobin) wrote:
Richard Heathfield <rj*@see.sig.invalidwrote:
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
Jun 27 '08 #60
Richard Harter said:
On 15 Apr 2008 12:42:23 -0700, Keith Thompson <ks*@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.

--
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
Jun 27 '08 #61
On 16 Apr, 09:21, Richard Heathfield <r...@see.sig.invalidwrote:
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
Jun 27 '08 #62
On 16 Apr, 07:11, Richard Heathfield <r...@see.sig.invalidwrote:
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
Jun 27 '08 #63
Antoninus Twink <no****@nospam.invalidwrites:
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)
Jun 27 '08 #64
ym******@gmail.com said:
On Apr 16, 3:37 am, Richard Heathfield <r...@see.sig.invalidwrote:
>ymunt...@gmail.com said:
On Apr 16, 3:11 am, Richard Heathfield <r...@see.sig.invalidwrote:
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 <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
Jun 27 '08 #65
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!

--
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
Jun 27 '08 #66
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

Jun 27 '08 #67
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?

--
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
Jun 27 '08 #68
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.
Jun 27 '08 #69
James Kuyper <ja*********@verizon.netwrote:
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
Jun 27 '08 #70
In article <49**********************************@u69g2000hse. googlegroups.com>,
<is*********@gmail.comwrote:
>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
Jun 27 '08 #71
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

Jun 27 '08 #72
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.

--
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
Jun 27 '08 #73
Richard Bos wrote:
James Kuyper <ja*********@verizon.netwrote:
>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.
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".
Jun 27 '08 #74
In article <l6nNj.9225$XF3.2026@trnddc04>,
James Kuyper <ja*********@verizon.netwrote:
>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
Jun 27 '08 #75
In article <gN******************************@comcast.com>,
Joe Wright <jo********@comcast.netwrote:
>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
Jun 27 '08 #76
is*********@gmail.com wrote:
On Apr 12, 1:16 am, Ian Collins <ian-n...@hotmail.comwrote:
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..

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 27 '08 #77
Richard Tobin wrote:
In article <l6nNj.9225$XF3.2026@trnddc04>,
James Kuyper <ja*********@verizon.netwrote:
>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.
Jun 27 '08 #78
Richard Heathfield <rj*@see.sig.invalidwrites:
[...]
[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"
Jun 27 '08 #79
James Kuyper <ja*********@verizon.netwrites:
Richard Tobin wrote:
>In article <l6nNj.9225$XF3.2026@trnddc04>,
James Kuyper <ja*********@verizon.netwrote:
>>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.
Jun 27 '08 #80
Keith Thompson wrote, On 16/04/08 17:59:
Richard Heathfield <rj*@see.sig.invalidwrites:
[...]
>[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
Jun 27 '08 #81
Richard wrote, On 16/04/08 18:58:
James Kuyper <ja*********@verizon.netwrites:
>Richard Tobin wrote:
>>In article <l6nNj.9225$XF3.2026@trnddc04>,
James Kuyper <ja*********@verizon.netwrote:

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.
Jun 27 '08 #82
Flash Gordon <sp**@flash-gordon.me.ukwrites:
Keith Thompson wrote, On 16/04/08 17:59:
>Richard Heathfield <rj*@see.sig.invalidwrites:
[...]
>>[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.
Your views on who is or is not a troll are off topic. Please take your
bitching offline to email where it belongs.

Jun 27 '08 #83
On 16 Apr 2008 at 9:52, Richard wrote:
Antoninus Twink <no****@nospam.invalidwrites:
>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)
You're right - this definition only works in practice, not in theory...

Jun 27 '08 #84
Richard wrote:
James Kuyper <ja*********@verizon.netwrites:
Richard Tobin wrote:
....
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.
I'm not sure what your point is - it would have been more helpful if
you had bothered to identify what makes it seem nonsensical to you.

I'll restrict my response to pointing out that your analogy is faulty.
I would use the word 'local' only to describe those variables which
have identifiers with function or block scope, and internal linkage. I
would only use the word 'global' to describe variables which have
identifiers with external linkage, regardless of scope. There's
clearly no overlap between those categories, but there's also quite
clearly many kinds of variables that don't fall into either category.
Therefore, I was endorsing the use of "non-local" as a term to
describe all of the variables that do not fall into the category
"local", whether or not they also fall into the category "global". The
appropriate analogy would be using using the term "non-black" to
describe white, asian and mixed-race people (among other
possibilities).

....
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.
OK. Now please accompany your example with a a language-independent
definition of "global variable" which applies to such a variable. A
list of examples is not a very good way to describe the elements of an
infinite set, or even one that is merely enormously large.
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.
It does, however, make the coffee cup less visible. If you want to
define what a "global variable" is in terms other than visibility,
that's fine. Please present the definition.
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.
In common usage, "global" doesn't mean "non-local", it means
"everywhere on the globe". Could a cell-phone company properly claim
to provide global coverage, if they only cover countries whose names
start with the letters 'A'-'M'? They're surely not just a local
company, but they're not yet big enough to be called "global",
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 don't know what you meant by that comment. I would like to have a
language-independent definition, but I know of no way to define one.
I'm willing to settle for a language-dependent definition, and I find
the particular language-dependent definition I've provided to be quite
useful.

If you think there's a better one, please provide it; but "I know one
when I see one" is pretty much the most useless definition I can
imagine.
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.
I would say that this is an accurate description of what I just did;
I'm using the generally accepted term, and I'm being clever enough to
understand that term in a manner that makes it useful in this context,
without being too clever. Obviously, you disagree, but it would be
more helpful if you'd explain why, and provide an alternative, rather
than merely rejecting it.
Jun 27 '08 #85
In article <fu**********@registered.motzarella.org>,
Richard <de***@gmail.comwrote:
>>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.
Non-local includes variables whose scope is less than global, for example
file-scope static variables in C and variables in containing scopes around
the recursive function in languages which support that (standard C doesn't
of course).

-- Richard
--
:wq
Jun 27 '08 #86
In article <7MpNj.7473$Ho5.3014@trnddc01>,
James Kuyper <ja*********@verizon.netwrote:
>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.
The version I gave yesterday, which said something like "visible given
a suitable declaration" seems to cover it. I take "suitable declaration"
to imply "and not an (unsuitable) shadowing one", but you can spell
that out if you like.

In any case, I don't think it matters if there is some amount of
ambiguity to the term. The context usually makes it clear, or if
it doesn't you can ask for clarification. That's what you'd do in
a normal conversation, and I don't see why discussions in comp.lang.c
should be any different.

-- Richard
--
:wq
Jun 27 '08 #87
Richard Harter <cr*@tiac.netwrote:
>
Variables, despite the name, don't have to vary.
Thus, the old saying: "Constants aren't. Variables won't."

-Larry Jones

I hope Mom and Dad didn't rent out my room. -- Calvin
Jun 27 '08 #88
ym******@gmail.com wrote:
>
As far as I know, the C standard does not define "variable".
It makes a normative reference to ISO/IEC 2382-1:1993, Information
Technology -- Vocabulary -- Part 1: Fundamental terms, which one would
expect to contain a definition. If anyone has access to that document,
it would be interesting to find out what it has to say.

-Larry Jones

I think your train of thought is a runaway. -- Calvin's Mom
Jun 27 '08 #89
Joe Wright <jo********@comcast.netwrote:
>
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.
Section A is DMR, not BWK.

-Larry Jones

Hello, I'm wondering if you sell kegs of dynamite. -- Calvin
Jun 27 '08 #90
Antoninus Twink <no****@nospam.invalidwrote:
>
You're right - this definition only works in practice, not in theory...
In theory, there's no difference between theory and practice, but in
practice, there is. :-)

-Larry Jones

Wheeee. -- Calvin
Jun 27 '08 #91
Richard Tobin wrote:
In article <gN******************************@comcast.com>,
Joe Wright <jo********@comcast.netwrote:
>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?
The canonical statement is:

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

Assuming success, I would say this allocates an array 10 of int.
I wouldn't think of, say, the third int in the array as a variable.
Of course x[2] (the third int) is 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 didn't realize we were discussing your preferences rather than
accepted definitions of the term 'variable'.
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 :-)
You have a 'very experienced colleague' who disagrees with Brian
Kernighan? And you side with him? Who pray tell is this genius?

Of course x is a variable. It is a pointer to int. x[0] through x[9] are
also discrete objects and variables of type int. There is nothing
disqualifying about x[2] as a name.
-- Richard
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 27 '08 #92
In article <yM******************************@comcast.com>,
Joe Wright <jo********@comcast.netwrote:
> int *x = malloc(10 * sizeof(x));
>The canonical statement is:

int *x = malloc(10 * sizeof *x);
The missing star was of course just a mistake.
>I didn't realize we were discussing your preferences rather than
accepted definitions of the term 'variable'.
Well, now you know.

-- Richard
--
:wq
Jun 27 '08 #93
On Apr 16, 1:24*pm, lawrence.jo...@siemens.com wrote:
ymunt...@gmail.com wrote:
As far as I know, the C standard does not define "variable".

It makes a normative reference to ISO/IEC 2382-1:1993, Information
Technology -- Vocabulary -- Part 1: Fundamental terms, which one would
expect to contain a definition. *If anyone has access to that document,
it would be interesting to find out what it has to say.
From this draft:
http://old.jtc1sc36.org/doc/36N0646.pdf

We have this:
3.5.2.3
variable
computational object to which a value of a particular datatype is
associated at any given time; and to which different values of the
same datatype may be associated at different times
Jun 27 '08 #94
Joe Wright <jo********@comcast.netwrites:
Richard Tobin wrote:
>In article <gN******************************@comcast.com>,
Joe Wright <jo********@comcast.netwrote:
>>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?
The canonical statement is:

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

Assuming success, I would say this allocates an array 10 of int.
Yes. Clearly.
>
>I wouldn't think of, say, the third int in the array as a variable.

Of course x[2] (the third int) is a variable.

It's an integer. Would you not say x is the 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 didn't realize we were discussing your preferences rather than
accepted definitions of the term 'variable'.
>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 :-)
You have a 'very experienced colleague' who disagrees with Brian
Kernighan? And you side with him? Who pray tell is this genius?
Brian Kernighan would be the first to admit he is not always right
either.
>
Of course x is a variable. It is a pointer to int. x[0] through x[9]
are also discrete objects and variables of type int. There is nothing
disqualifying about x[2] as a name.
Are you sure they are not simply allocations in which to store
variables?

You know, just to play the word games prevailent in c.l.c....
>
>-- Richard
Jun 27 '08 #95
la************@siemens.com said:
ym******@gmail.com wrote:
>>
As far as I know, the C standard does not define "variable".

It makes a normative reference to ISO/IEC 2382-1:1993, Information
Technology -- Vocabulary -- Part 1: Fundamental terms, which one would
expect to contain a definition. If anyone has access to that document,
it would be interesting to find out what it has to say.
You mean you don't?!? You're on the Committee, right? And, according to my
C89 Draft, you (or someone with a remarkably similar name) was on the
Committee right back in C89 days.

My observation is this: how is it possible for a Standard to make a major
normative reference to a fundamental document without at least those
committee members who are able to vote on the inclusion or otherwise of
that reference having good access to, and a reasonable working knowledge
of, that document?

--
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
Jun 27 '08 #96
Richard Tobin wrote:
In article <7MpNj.7473$Ho5.3014@trnddc01>,
James Kuyper <ja*********@verizon.netwrote:
>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.

The version I gave yesterday, which said something like "visible given
a suitable declaration" seems to cover it. I take "suitable declaration"
to imply "and not an (unsuitable) shadowing one", but you can spell
that out if you like.
I don't think it's a good idea to define the term in terms of possible
modified versions of the code; it should be definable in terms of the
code as actually written.

Furthermore, shadowing isn't a language-independent concept; for that
matter, neither is declaration.

I also don't like the fact that the presence of a shadowing declaration
renders a variable non-global. With my definition, it remains global, it
just can't be used within the shadow.
Jun 27 '08 #97
In article <EBzNj.11777$El4.7040@trnddc05>,
James Kuyper <ja*********@verizon.netwrote:
>The version I gave yesterday, which said something like "visible given
a suitable declaration" seems to cover it. I take "suitable declaration"
to imply "and not an (unsuitable) shadowing one", but you can spell
that out if you like.
[...]
>I also don't like the fact that the presence of a shadowing declaration
renders a variable non-global.
My definition doesn't do that. It says it is global if it is visible
everywhere where {there's a suitable declaration and it isn't shadowed}.

-- Richard
--
:wq
Jun 27 '08 #98
In article <fu**********@registered.motzarella.org>,
Richard <de***@gmail.comwrote:
>You have a 'very experienced colleague' who disagrees with Brian
Kernighan? And you side with him? Who pray tell is this genius?
>Brian Kernighan would be the first to admit he is not always right
either.
It's not an issue of right and wrong. My intention was to show the
range of ways the term is used, and to suggest that Kernighan's was at
one end of that range.

-- Richard
--
:wq
Jun 27 '08 #99
On Apr 16, 5:29 pm, Joe Wright <joewwri...@comcast.netwrote:
Richard Tobin wrote:
In article <gNSdnRYJHJ7QipvVnZ2dnUVZ_gqdn...@comcast.com>,
Joe Wright <joewwri...@comcast.netwrote:
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?

The canonical statement is:

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

Assuming success, I would say this allocates an array 10 of int.
I wouldn't think of, say, the third int in the array as a variable.

Of course x[2] (the third int) is 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 didn't realize we were discussing your preferences rather than
accepted definitions of the term 'variable'.
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 :-)

You have a 'very experienced colleague' who disagrees with Brian
Kernighan? And you side with him? Who pray tell is this genius?
In fact you are misinterpreting the words of Kernighan:
"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."
For storage class you need a declaration, and that's variables:
in "int a[10];" the variable "a" has storage class, not the
object '*(char*)(a[2])'. And if you think this quoted text
says that the block of memory allocated by malloc() is a
variable, you simply have (too) strong imagination.

malloc(10); // variable leak!
Of course x is a variable. It is a pointer to int. x[0] through x[9] are
also discrete objects and variables of type int.
So, the first byte of x[2] is what (note, the *byte*, not
a char object or something, just that single byte region
of storage)? It's an object by definition. Is it a variable?
Of what type?
There is nothing
disqualifying about x[2] as a name.
"x[2]" is a name?

Yevgen
Jun 27 '08 #100

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

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

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