How to eliminate this global variable, silent?

K

Keith Thompson

Richard said:
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.
 
A

Antoninus Twink

Richard Harter said:

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;
}
 
K

Keith Thompson

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

Keith Thompson

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

Willem

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
 
A

Antoninus Twink

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"?
 
K

Keith Thompson

[email protected] (Richard Harter) said:
On Tue, 15 Apr 2008 09:49:44 -0700, Keith Thompson

[...]
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 <[email protected]>, 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.

[...]
 
R

Richard Tobin

Richard Heathfield said:
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
 
L

lawrence.jones

Keith Thompson said:
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
 
Y

ymuntyan

Ben Bacarisse said:





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
 
K

Keith Thompson

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

istillshine

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 avoid using them. If so, why creating global
variables? They must be useful in certain circumstances.
 
I

istillshine

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

Keith Thompson

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

Ian Collins

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

istillshine

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()".
 
I

Ian Collins

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()".
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.
 
Y

ymuntyan

(e-mail address removed) 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
 
B

Ben Bacarisse

On 15 Apr 2008 13:31:57 -0700, Keith Thompson <[email protected]>
wrote:

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top