F
fabio
Why? i' ve heard about this, the usage of global vars instead of
locals is discouraged, but why?
thx
locals is discouraged, but why?
thx
fabio said:Why? i' ve heard about this, the usage of global vars instead of
locals is discouraged, but why?
There is no such thing as a global variable in C.
http://www.google.com/search?q=global+variables+in+c
What you probably mean is a variable with static storage duration
and external linkage. Why make such a fuss about denomination? The
storage duration and the linkage determine different sides of
"global".
Richard G. Riley said:
http://www.google.com/search?q=tooth+fairy (nearly four million hits).
Just cos it's on Google, doesn't mean it exists.
Richard Heathfield said:http://www.google.com/search?q=tooth+fairy (nearly four million hits).
Just cos it's on Google, doesn't mean it exists.
"Richard"posted the following on 2006-03-11:
You dont say? But when we find x thousand courses which are quite
happy to see "global variable" talked about in C then does it seem
quite right to start pretending it doesnt apply anymore in ng?
No. Did
you read the initial reply?
Yes.
Did you think that was constructive for a newbie?
Yoou dont know or agree with the common meaning of "global variable" in
C?
Neither does "linked list". Or "pure function". Doesn't mean that such aRichard Heathfield said:The term has no universally recognised meaning in C.
Richard G. Riley said:
The word "variable" isn't all that useful, and the term "global variable" is
less useful still.
Yes.
Yes. No point lyin' to 'em.
The term has no universally recognised meaning in C.
Richard said:I did not think that reply in anyway
clarified anything.
One has to be practical. Not too clever for ones own good sometimes.
Neither do lots of things : but the art is in recognising the meaning
from a newbie. Any half decent programmer worth his salt knows and
recognises what is meant by a global variable and in that context C
certainly does support them and the reasons for not using them are as
prevalent as in other languages : which is, fater all, what the OP was
asking. I dont know, maybe I'm getting too old for this : there was a
time when people wanted to help : not just get one upsmanship points
on who can read the standards the best.
"Richard"posted the following on 2006-03-11:
... You dont know or agree with the common meaning of
"global variable" in C?
You could have posted a direct reply.
You just could have expanded on mine and said that you consider
it too technical to be of any use to a newbie -- and be done.
For me, it was important to clarify that there are three
conceptual sides to it:
- Linkage
- Storage duration
- Ease or difficulty of seeing whether the implemented design
is the intended one, let alone correct
The former two are joined to the latter but may give toothaches
on their own. If I failed to carry this point over, then shame
on me. If you understood it but did not clarify it to suffice
your standards, shame on you.
In addition, it _is_ important to know what you are talking
about. The concepts mentioned should help the OP find his way
through it and also find out which side leads to which problem.
It may very well be that he decides to use a "global" variable
with internal linkage because this is exactly what he needs --
after having thought about why he does not want the uncertainties
involved in an object with external linkage and potential problems
when linking.
This is true.
Nothing ever is simple, though, so I consider it fair to tell
people a little bit more.
A perfect reply certainly would be one of Chris Torek type (useful,
clear, concise, written nicely) but I certainly am not up to that
as I am neither a teacher nor a native speaker.
The latter was not my intention. I post here to help and to
learn. I certainly do not pride myself on my often spotty knowledge
of the standard. You will guaranteedly find a couple of times
within the last month where I made a mistake and was glad because
it was corrected.
If you are saying that my reply was insufficient: You were free
to improve it. Next time I will take what you said into account
when answering a similar question.
If you are saying that I did it intentionally to show off, then I
think you are just once more out to look for an unnecessary fight
to pick -- and you are plain wrong.
Richard said:I didnt : I didnt doubt the contents one bit. But at the end of the
day we know what is meant. It is a practical understanding that
evolves from knowing languages and what new programmers are really asking.
He was asking more about the use of them : not the issues with the
underlying system - unless I got the wrong end of the stick in which
case I apologise-
My own thought is that it was way too technical for someone asking
about global and local variables in programming.
I'm not disputing the accuracy of you post. That is not my intent.
No : but sometimes one must step back and consider who we are replying
to. Your reply was very, very technical IMO. And also it is unfair to
suiggest that "global variables do not exist in C". I still maintain
they do for the purposes of describing structured programming in C.
Neither does "linked list". Or "pure function". Doesn't mean that such a
think doesn't exist.
I dont know, maybe I'm getting too old for this : there was a
time when people wanted to help : not just get one upsmanship points
on who can read the standards the best.
Richard G. Riley said:
The word "variable" isn't all that useful, and the term "global variable" is
less useful still.
Yes. No point lyin' to 'em.
The term has no universally recognised meaning in C.
(I think this is a bit of an overstatement, but
"Richard"posted the following on 2006-03-11:
... You dont know or agree with the common meaning of
"global variable" in C?
I do not know about the other Richard; but the problem I have with
the phrase is that there seem to be at least two *different*
"common meanings" for that phrase in C (there may be more, but
I have only seen two "commonly expressed" ones).
One refers to static-duration external-linkage objects:
% grep varx *.[ch]
foo.h: extern int varx;
bar.c: varx++;
foo.c: int varx = 42;
[etc]
These have uncontrolled access and "singleton" behavior (to borrow
terminology usually used with C++ and similar inferior attempts
at OO languages).
Another refers merely to static-duragion objects, which may have
only internal linkage:
% grep didinit *.[ch]
bar.c: static int didinit;
bar.c: we use didinit to gripe rather than auto-init'ing in order to
bar.c: if (!didinit) gripe("missing call to init routine");
bar.c: if (!didinit) gripe("missing call to init routine");
bar.c: didinit = 1;
bar.c: if (!didinit) gripe("missing call to init routine");
(people generally do not call these "global" if they are block-scope,
but do often call them "global" if they are file-scope). Variables
like this tend to be less objectionable; they have much better
controlled access, at least.
Richard G. Riley said:
I advocate sticking to the terminology of the Standard not because I want to
score one-upmanship points (I already have plenty of those and to spare),
but because, in my experience, people learn to write portable C more
effectively if they can understand that terminology and thus apply the
information in the Standard to their own programming. This is why I
constantly refer to objects rather than variables, implicit conversions
rather than "implicit casts", and so on.
What about external-linkage variables with block scope? [One could argue
that an external-linkage variable has an existence in its own right,
independent of a mere declaration, but is it "global"?]
What about external-linkage variables with block scope? [One could argue
that an external-linkage variable has an existence in its own right,
independent of a mere declaration, but is it "global"?]
This is why I said "at least" two: it depends on how fine you want
to make your various distinctions.
To get an external-linkage block-scope variable in C, we have to
use the "extern" keyword:
void f(void) {
extern int somevar;
...
}
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.