How to eliminate this global variable, silent?

I

istillshine

Following your suggestion, I wrote

static int silent; /* global in main.c */
int getSilent()
{
return silent;
}
int main(int argc, char *argv[])
{
if (some condition)
silent = 1;
}

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.

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

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

Ian Collins

*Please* don't quote signatures.
Suppose I have 100 functions in my program, is it a good idea to put
all their declarations in a header file called proto.h?
Not really, why would you? If all of those functions form a public
interface, you might want to exposes them. Mode likely...
Another way maybe putting them into separate .h files, i.e., putting
all function declarations defined in a.c into a.h, all function
declarations defined in b.c into b.h, and so on.

....you would want to group your non-static functions in a number of headers.
But including a main.h (containing getSilent()) for every .c is not a
good idea.
Why not? The name's a bit naff, but putting all your debug related
declarations in something like "debug.h" and including it where required
is perfectly sound practice.
 
B

Ben Bacarisse

If you simply want to remove the global, make silent static
and expose it through a getSilent() function.
Following your suggestion, I wrote

static int silent; /* global in main.c */
int getSilent()
{
return silent;
}
int main(int argc, char *argv[])
{
if (some condition)
silent = 1;
}

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.

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

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

If you just mean 100 functions that are being offered to the other
parts of the program, then I suggest you try to group them into
manageable "topics" -- logging functions, statistical functions, tree
functions, I/O, etc. with a .h (and .c for each group).
Another way maybe putting them into separate .h files, i.e., putting
all function declarations defined in a.c into a.h, all function
declarations defined in b.c into b.h, and so on.

That is a very good way to do it. Especially if the names convey the
related nature of the functions.
But including a main.h (containing getSilent()) for every .c is not a
good idea.

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

istillshine

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

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

int getSilent()

to let compilation pass?

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

Ian Collins

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

int getSilent()

to let compilation pass?

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

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

ymuntyan

(e-mail address removed) 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.
Okay, what *is* it equivalent to, in the C language? Please justify your
answer with reference to the language definition.

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

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

Sure, you don't give a shit to what many other
people think. That's natural, and I didn't
say you should or you do care.
The C Standard is nonsense now?

Straw man. I didn't say the C standard is nonsense.
Your "arguments" were nonsense. That you used words
which appear in the standard doesn't mean you backed
something up by the standard.
Right. So what *do* you mean by "global variable"? Nobody, as far as I can
see, has yet disambiguated this term to everyone's satisfaction. Until
this is done, the term "global variable" remains ambiguous,

True. This one is very clear. Who argues about it?
and therefore
should not be used without at the very least being defined by the user on
each first usage within an article.

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

What? Read what you snipped again, about scopes of objects.
It's your business to "do research" and whatnot, you're the
smart folk who know the language definition here.
If you really think I think that, it says a lot more about you than it does
about me.

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

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

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

Yevgen
 
Y

ymuntyan

(e-mail address removed) said:



Firstly, what do you mean by "global variable"? Do you mean file scope
objects of any kind? File scope objects with external linkage?

Could you define what "file scope objects" mean please?
With reference to the language definition of course.
File scope
objects to which you expose update rights over a wide area network?

Do you think the OP could mean that?

Yevgen
 
Y

ymuntyan

(e-mail address removed) said:



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

ymuntyan

(e-mail address removed) said:



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


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?



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
 
Y

ymuntyan

(e-mail address removed) said:



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.
You start - give me *one* definition of "global variable".

<snip>

Yevgen
 
R

Richard Bos

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
 
R

Richard Bos

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
 
Y

ymuntyan

(e-mail address removed) said:



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

Oh yes I did say what I was talking about. About
word games, about someone who invents his terms
and claims they are standard and "finds" quotes
from the standard to back it up, about someone
being a smart ass, and so on. You got the idea
perfectly well, I am sure. And about wide network
and snipping too, by the way.
Of course it's obvious - to anyone who has read the thread. And to anyone
who hasn't, I'll make my point clear right now - what do you MEAN by
global variables?

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

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

Yevgen
 
N

Nick Keighley

Richard Harter said:
On 15 Apr 2008 12:42:23 -0700, Keith Thompson <[email protected]>
It's a "poking a little fun at Heathfield" thing, referring to a
dogmatic passage in one of his posts.  I'll be good and stop.

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

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

Nick Keighley

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

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

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

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

ymuntyan

(e-mail address removed) said:



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

I did not. Did I say I did?
You mean "global variable"?

No, "file scope object".
Oh, you mean "file scope objects". You don't seem to have any difficulty
understanding what I meant by the term, as you yourself have agreed.

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

int a[10];

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

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

Yes we can. Given a good will. I can talk to someone,
and I can ask him to clarify what he means in case of
misunderstanding. But only you can pull "wide network"
out of "global variable".
How do you know? We don't have a definition for the term yet.

How do I know? I don't. How do I know that there
are no aliens in my fridge? I don't. But I tell
you what, there are no aliens in my fridge. And
the OP didn't mean something related to "wide
network". Now, you didn't say a single time (even
though you were asked few times) that you did think
the OP *could* mean anything like that. Of course,
you didn't think so! But, "we don't have a definition".
Yeah.
I'm still trying to find out what you mean by "global variable", but all
you do is come up with insults and evasions. I am beginning to suspect
that you don't actually know what you mean.

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

Yevgen
 
N

Nick Keighley

(e-mail address removed) said:

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?



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


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
 
R

Richard

Antoninus Twink said:
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)
 
C

Chris Dollin

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

- it identifies a common form of coupling between components.
Shorthand for what? :)

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

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,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top