By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 1,475 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

How to eliminate this global variable, silent?

P: n/a
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 #1
Share this Question
Share on Google+
112 Replies


P: n/a
On Fri, 11 Apr 2008 21:31:41 -0700 (PDT), is*********@gmail.com wrote:
>When I control if I print messages, I usually use a global variable
"int silent". When I set "-silent" flag in my command line
parameters, I set silent = 1 in my main.c.
I have many functions that may print some messages.

foo(...)
{
if (!silent)
printf("Msg1\n");
}
foo2(...)
{
if (!silent)
printf("Msg2\n");
}
and so on...
Is the above bad coding practice? How to eliminate the variable
"silent" but achieve the same effect?
If you are using for debugging, it is fine.
Remove del for email
Jun 27 '08 #2

P: n/a
is*********@gmail.com wrote:
When I control if I print messages, I usually use a global variable
"int silent". When I set "-silent" flag in my command line
parameters, I set silent = 1 in my main.c.
I have many functions that may print some messages.

foo(...)
{
if (!silent)
printf("Msg1\n");
}
foo2(...)
{
if (!silent)
printf("Msg2\n");
}
and so on...
Is the above bad coding practice?
No, it's quite common for debugging.
How to eliminate the variable "silent" but achieve the same effect?
Create a wrapper for printf and link with either a debug or production
version. If you simply want to remove the global, make silent static
and expose it through a getSilent() function.

--
Ian Collins.
Jun 27 '08 #3

P: n/a
Hi,
Here is modification for your program.
--------------
foo()
{
#if !SILENT
printf("Msg1\n");
#endif

}

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

-- Pradeep
On Apr 12, 10:16 am, Ian Collins <ian-n...@hotmail.comwrote:
istillsh...@gmail.com wrote:
When I control if I print messages, I usually use a global variable
"int silent". When I set "-silent" flag in my command line
parameters, I set silent = 1 in my main.c.
I have many functions that may print some messages.
foo(...)
{
if (!silent)
printf("Msg1\n");
}
foo2(...)
{
if (!silent)
printf("Msg2\n");
}
and so on...
Is the above bad coding practice?

No, it's quite common for debugging.
How to eliminate the variable "silent" but achieve the same effect?

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

--
Ian Collins.
Jun 27 '08 #4

P: n/a
On Apr 12, 1:16 am, Ian Collins <ian-n...@hotmail.comwrote:
Create a wrapper for printf and link with either a debug or production
version.
Suppose the wrapper name is called info. How to implement it? I
tried to write it,as shown below. But failed to do it.
#ifdef DEBUG
#define info() /* how ? */
#else
#define info() /* how? */
#endif /* DEBUG */
Jun 27 '08 #5

P: n/a
is*********@gmail.com wrote:
On Apr 12, 1:16 am, Ian Collins <ian-n...@hotmail.comwrote:
>Create a wrapper for printf and link with either a debug or production
version.

Suppose the wrapper name is called info. How to implement it? I
tried to write it,as shown below. But failed to do it.
#ifdef DEBUG
#define info() /* how ? */
#else
#define info() /* how? */
#endif /* DEBUG */
Are you the same "istillshine" who in another thread
is lecturing us all about the use of `const'? If so, I
hope you'll pardon me for declining to accept advice from
someone who can't even read the FAQ. Question 10.26, to
be specific.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #6

P: n/a
On Apr 12, 10:46 am, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
Are you the same "istillshine" who in another thread
is lecturing us all about the use of `const'?
I think "lecturing" was not my attitude when I posted that thread. If
it appeared to be so, I am sorry. I posted it because I felt confused
on that point. I wanted to see what other people think.

If so, I
hope you'll pardon me for declining to accept advice from
someone who can't even read the FAQ. Question 10.26, to
be specific.
Oh it's there. I did not notice it. Thank you for pointing it out to
me.

Jun 27 '08 #7

P: n/a
Pradeep <pr**************@gmail.comwrites:
Here is modification for your program.
--------------
foo()
{
#if !SILENT
printf("Msg1\n");
#endif
}
<snip more>

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

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

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

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

program_options.silent

is not much harder to use than

program_options_silent

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

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

--
Ben.
Jun 27 '08 #8

P: n/a
Ben Bacarisse <be********@bsb.me.ukwrites:
[...]
[1] Can we not permit the phrase "global variable" into c.l.c to mean
an object defined at file scope with external linkage? It would save
a lot of typing.
[...]

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

Hypothetical example:

QI've heard that global variables are a bad idea, but I think
Qthey're really cool.

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

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

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

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

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

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

P: n/a

Keith Thompson <ks***@mib.orgwrites:
Even though C doesn't define either "global" or "variable", I don't
mind using either or both as a convenient shorthand *as long as* the
usage is unambiguous (or at least as unambiguous as it needs to be).
What a load of hot air.

It should be apparent to an sentient being what a "variable" is in a
programming newsgroup. Even this one. Ditto for "global".
Jun 27 '08 #10

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

-Larry Jones

OK, there IS a middle ground, but it's for sissy weasels. -- Calvin
Jun 27 '08 #11

P: n/a
la************@siemens.com writes:
Richard <de***@gmail.comwrote:
>>
It should be apparent to an sentient being what a "variable" is in a
programming newsgroup. Even this one. Ditto for "global".

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

--
Ben.
Jun 27 '08 #12

P: n/a
Richard <de***@gmail.comwrites:
la************@siemens.com writes:
>Richard <de***@gmail.comwrote:
>>>
It should be apparent to an sentient being what a "variable" is in a
programming newsgroup. Even this one. Ditto for "global".

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

No. Because its file scope. Not global scope. I don think I'm being
overly simplistic here.
You (correctly in my opinion) exclude it, but you should exclude it
because it does not have external linkage. This variable:

int x;

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

--
Ben.
Jun 27 '08 #13

P: n/a
Ben Bacarisse said:
Richard <de***@gmail.comwrites:
>la************@siemens.com writes:
>>Richard <de***@gmail.comwrote:

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

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

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

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

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

P: n/a
On Tue, 15 Apr 2008 04:54:21 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>Ben Bacarisse said:
>Richard <de***@gmail.comwrites:
>>la************@siemens.com writes:

Richard <de***@gmail.comwrote:
>
It should be apparent to an sentient being what a "variable" is in a
programming newsgroup. Even this one. Ditto for "global".

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

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

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

Excluding it because it doesn't have global scope is perfectly correct.
"There are four kinds of scopes: function, file, block, and function
prototype", quoth the Standard. Since no object can be considered global,
even by a troll, unless it has global scope, and since there is no such
thing as global scope in C, *therefore* C doesn't have global objects (or
"variables", as some people like to call them). And even the trolls agree
with this.
For some particular breed of troll, of course. Some of us trolls
wouldn't use that definition for global.
Richard Harter, cr*@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
Jun 27 '08 #15

P: n/a
cr*@tiac.net (Richard Harter) writes:
On Tue, 15 Apr 2008 04:54:21 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>>Ben Bacarisse said:
>>Richard <de***@gmail.comwrites:

la************@siemens.com writes:

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

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

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

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

For some particular breed of troll, of course. Some of us trolls
wouldn't use that definition for global.
Heathfield is playing silly games. C has global variables and no amount
of silly word games will change that.
Jun 27 '08 #16

P: n/a
la************@siemens.com writes:
Richard <de***@gmail.comwrote:
>It should be apparent to an sentient being what a "variable" is in a
programming newsgroup. Even this one. Ditto for "global".
Mr. Riley, perhaps you'd care to keep the personal insults to
yourself.
"Variable" shouldn't be controversial, even the C Standard uses the
term. But "global variable" is a slipperier concept in C. For example,
does a file scope static variable qualify or not? It's not exactly a
local variable, but it's not exactly a global variable, either.
Even "variable" isn't clearly defined -- and the standard only uses it
as an adjective or in an informal (in particular, non-normative)
context. I think we can all agree that a single declared non-const
object is a "variable", and I have no problem using the term
informally in cases like that where it's unambiguous. But what about
a const-declared object? What about a member of a struct or union?
What about an element of an array? What about an object allocated by
malloc()?

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

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

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

--
Keith Thompson (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 #17

P: n/a
Richard <de***@gmail.comwrites:
[...]
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.

--
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 #18

P: n/a
In article <87************@kvetch.smov.org>,
Keith Thompson <ks***@mib.orgwrote:
>C has global variables and no amount of silly word games will change
that.
>Please provide a complete and unambiguous definition of the phrase
"global variable" using terms defined by the C standard.
I suggest it is a variable that can, given a suitable declaration, be
made visible anywhere in the program. (If you object that the term
"variable" is not defined, then presumably you will also object
to that word's use in comp.lang.c too.)

So the variable defined by the top-level definition

int x;

is global, because you can declare

extern int x;

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

The variable defined by the top-level definition

static int x;

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

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

-- Richard
--
:wq
Jun 27 '08 #19

P: n/a
Richard Tobin said:
In article <87************@kvetch.smov.org>,
Keith Thompson <ks***@mib.orgwrote:
>>C has global variables and no amount of silly word games will change
that.
>>Please provide a complete and unambiguous definition of the phrase
"global variable" using terms defined by the C standard.

I suggest it is a variable that can, given a suitable declaration, be
made visible anywhere in the program.
Give a suitable declaration, and I'll show you a part of a C program where
this "variable" is not visible. The technique is obvious:

extern int x;

double foo(void)
{
double x = 3.14;
/* int x is not visible here */
return x;
}
(If you object that the term
"variable" is not defined, then presumably you will also object
to that word's use in comp.lang.c too.)
Right. :-)
--
Richard 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 #20

P: n/a
Keith Thompson <ks***@mib.orgwrites:
Richard <de***@gmail.comwrites:
[...]
>C has global variables and no amount of silly word games will change
that.

Please provide a complete and unambiguous definition of the phrase
"global variable" using terms defined by the C standard. If you
manage to provide a definition we can all agree on, I won't object to
the use of the term.
I dont care if you object or not. C has global variables. However you
want to obfuscate it by referring to the standard. This group is not C89
you know. Or C99.

It's about C.

Whether you like it or not.

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

To anyone who speaks English this is a global variable.

But then I inhabit the real world where people talk about real things
and not some dusty, confusing paragraph in a standard.
Jun 27 '08 #21

P: n/a
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <87************@kvetch.smov.org>,
Keith Thompson <ks***@mib.orgwrote:
>>C has global variables and no amount of silly word games will change
that.
>>Please provide a complete and unambiguous definition of the phrase
"global variable" using terms defined by the C standard.

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

So the variable defined by the top-level definition

int x;

is global, because you can declare

extern int x;

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

The variable defined by the top-level definition

static int x;

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

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

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

Jun 27 '08 #22

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

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Jun 27 '08 #23

P: n/a
Richard <de***@gmail.comwrote:

<snip don't care>
But then I inhabit the real world where people talk about real things
and not some dusty, confusing paragraph in a standard.
Do you really find the standard confusing?

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Jun 27 '08 #24

P: n/a
On Tue, 15 Apr 2008 09:49:44 -0700, Keith Thompson
<ks***@mib.orgwrote:
>Richard <de***@gmail.comwrites:
[...]
>C has global variables and no amount of silly word games will change
that.

Please provide a complete and unambiguous definition of the phrase
"global variable" using terms defined by the C standard. If you
manage to provide a definition we can all agree on, I won't object to
the use of the term.
Someone, I can't recall who, it must have been some other Keith,
gave a quite nice definition. Still, one can quibble about
definitions. Stepping outside of C for a moment, there are (or
can conceived of being) two general categories of globals -
program wide globals and module wide globals, and two major
visibility rules - automatically visible and visible only with a
declaration. C doesn't have modules as such but files, er,
translation units play much the same role, and file scope
variables are, in effect, module wide globals. So, C in effect
has two types of globals, program wide globals visible with a
declaration and translation unit wide globals automatically
visible.

As for defining variables, providing a definition using the
language of the C standard is quite beyond. Roughly speaking C
variables are identifiers bound both to a storage declaration and
(potentially) to a storage object. There are corner cases, of
course, but then any adequate description of C is all corner
cases.
Richard Harter, cr*@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
Jun 27 '08 #25

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

I dont care if you object or not. C has global variables. However you
want to obfuscate it by referring to the standard. This group is not C89
you know. Or C99.
Referring to the standard is obfuscation? That's a new one.
It's about C.

Whether you like it or not.

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

To anyone who speaks English this is a global variable.

But then I inhabit the real world where people talk about real things
and not some dusty, confusing paragraph in a standard.
I'd be glad to explain any passages that you find confusing.

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

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

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

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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #26

P: n/a
On 15 Apr 2008 at 19:13, Richard Heathfield wrote:
Richard Harter said:

<snip>
>Some of us trolls think that you're quibbling,
As time goes by, more and more sensible people feel themselves excluded
as "trolls".
Quibbling is sometimes how we get to the truth.
If you say so.

extern int x;

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

Jun 27 '08 #27

P: n/a
cr*@tiac.net (Richard Harter) writes:
On Tue, 15 Apr 2008 09:49:44 -0700, Keith Thompson
<ks***@mib.orgwrote:
Richard <de***@gmail.comwrites:
[...]
C has global variables and no amount of silly word games will change
that.
Please provide a complete and unambiguous definition of the phrase
"global variable" using terms defined by the C standard. If you
manage to provide a definition we can all agree on, I won't object to
the use of the term.

Someone, I can't recall who, it must have been some other Keith,
gave a quite nice definition.
Are you suggesting that I've given a definition of "global variable"?
If so, I honestly don't remember it; can you provide a reference (URL
or message-id)?

[...]
As for defining variables, providing a definition using the
language of the C standard is quite beyond.
Quite beyond what? I don't understand what you mean by that.
Roughly speaking C
variables are identifiers bound both to a storage declaration and
(potentially) to a storage object. There are corner cases, of
course, but then any adequate description of C is all corner
cases.
Your (admittedly rough) definition implies that a variable is an
identifier. I would have thought that a variable is the object that
an identifier refers to.

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

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

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

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

P: n/a
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
Jun 27 '08 #29

P: n/a
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"?

Jun 27 '08 #30

P: n/a
On 15 Apr 2008 12:32:10 -0700, Keith Thompson <ks*@cts.com>
wrote:
>cr*@tiac.net (Richard Harter) writes:
>On Tue, 15 Apr 2008 09:49:44 -0700, Keith Thompson
<ks***@mib.orgwrote:
>Richard <de***@gmail.comwrites:
[...]
C has global variables and no amount of silly word games will change
that.

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

Someone, I can't recall who, it must have been some other Keith,
gave a quite nice definition.

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

I can't find the message - there is simply too much stuff in
c.l.c - and perhaps it was someone else, but the style was yours.
The essence was something like "if you want to define a global as
a file scope variable with extern qualifier then we can talk
about globals in C". Does that sound familiar.
>
[...]
>As for defining variables, providing a definition using the
language of the C standard is quite beyond.

Quite beyond what? I don't understand what you mean by that.
"quite beyond my feeble mind", which is understandable since it
seems that actually typing what I am thinking of is quite beyond
my feeble mind.
>
> Roughly speaking C
variables are identifiers bound both to a storage declaration and
(potentially) to a storage object. There are corner cases, of
course, but then any adequate description of C is all corner
cases.

Your (admittedly rough) definition implies that a variable is an
identifier. I would have thought that a variable is the object that
an identifier refers to.
Bad wording on my part - I meant that a variable a three in one
kind of thing, the three parts being bound together. All three
parts are part of the package. That said, it is the contents of
the object that can vary.
>
If "variable" is such a simple concept (and I'm not arguing that it
isn't), then it shouldn't be difficult to define it in standard terms.
And I have no problem using the term informally when there's no
ambiguity. For example, given:

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

the object named "var" is clearly a variable. But yes, it's the
corner cases I'm concerned about. And even if we nail down what a
"variable" is, the phrase "global variable" presents even more corner
cases, something that Mr. Riley refuses to acknowledge.
Richard Harter, cr*@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
Jun 27 '08 #31

P: n/a
cr*@tiac.net (Richard Harter) writes:
On 15 Apr 2008 12:32:10 -0700, Keith Thompson <ks*@cts.com>
wrote:
cr*@tiac.net (Richard Harter) writes:
On Tue, 15 Apr 2008 09:49:44 -0700, Keith Thompson
<ks***@mib.orgwrote:

Richard <de***@gmail.comwrites:
[...]
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 <87************@kvetch.smov.org>, available at
<http://groups.google.com/group/comp.lang.c/msg/02c5c1b781def71e>. It
was actually Ben Bacarisse who proposed the definition.

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

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

[...]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #32

P: n/a
In article <Tf*********************@bt.com>,
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.

-- Richard
--
:wq
Jun 27 '08 #33

P: n/a
On 15 Apr 2008 13:31:57 -0700, Keith Thompson <ks*@cts.com>
wrote:
>cr*@tiac.net (Richard Harter) writes:
>On 15 Apr 2008 12:32:10 -0700, Keith Thompson <ks*@cts.com>
wrote:
>cr*@tiac.net (Richard Harter) writes:
On Tue, 15 Apr 2008 09:49:44 -0700, Keith Thompson
<ks***@mib.orgwrote:

Richard <de***@gmail.comwrites:
[...]
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 <87************@kvetch.smov.org>, available at
<http://groups.google.com/group/comp.lang.c/msg/02c5c1b781def71e>. It
was actually Ben Bacarisse who proposed the definition.
Mea culpa. Ben has now been promoted to a Keith.
>
But I'm not sure that definition is really suitable, for reasons I
mentioned in that article: it means a const-qualified object can be a
global "variable", even though it's not able to vary, and it obscures
the very important distinction between scope and storage duration.
Variables, despite the name, don't have to vary. I don't agree
that the term "global" blurs the distinction; a global, assuming
that we agree on its usage in the context of C, is a fairly well
defined thing that has a definite kind of scope and a definite
kind of storage.
>
The folks here who insist most loudly that everyone knows what a
"global variable" is are unwilling or unable to offer a definition.
I'll just have to assume, until they demonstrate otherwise, that if
they can't define the term they don't really understand it.
You can assume that if you like, of course. I, on the hand, can
appreciate that they might think it quite obvious what is meant
by the term, and think the various objections are mere
pettigfoggery. I might even agree with them; that said, I agree
(I think) that the discussion would be all the better for
definitions.

I like Ben's definition, modulo corner cases. It captures what
people usually mean by globals.
Richard Harter, cr*@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
Jun 27 '08 #34

P: n/a
cr*@tiac.net (Richard Harter) writes:
[...]
As a side note, by my lights Twink et al are not trolls - they
are critics. A troll proper posts rubbish with the object of
raising controversy. The critics genuinely object to the c.l.c
culture. My objection to them is that their criticism is
inconsequential and ineffective. In consequence they are as much
a waste of bandwidth as the net-nannies.
Hmm. You may be right, but I don't see it that way. Personally, I
don't give them that much credit. I don't believe they genuinely
object to the culture, or if they do, I don't believe that's the
primary motivation behind their actions. But in any case, I don't
particularly care what they think about the culture. As far as I can
tell, they're just being obnoxious jerks because they think it's fun.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #35

P: n/a
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.
Jun 27 '08 #36

P: n/a
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.
Jun 27 '08 #37

P: n/a
is*********@gmail.com wrote:
I have several further questions on global variables.

When are they used? I.e., in what situations it is good to use them.
Hardly any.
People are talking about avoiding using them. If so, why creating
global
variables? They must be useful in certain circumstances.
C carries a lot of historical baggage. Everywhere a global can be used,
an alternative approach can be used.

--
Ian Collins.
Jun 27 '08 #38

P: n/a
On Apr 15, 7:13 pm, Ian Collins <ian-n...@hotmail.comwrote:
Everywhere a global can be used,
an alternative approach can be used.
Good to know that. Could you give a few concrete examples, beside the
"silent" one?
Jun 27 '08 #39

P: n/a
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()".

Jun 27 '08 #40

P: n/a
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()".
Place the function declaration in a shared header. That way, you can
expose the value of silent and restrict write access to the file where
you define it.

--
Ian Collins.
Jun 27 '08 #41

P: n/a
cr*@tiac.net (Richard Harter) writes:
On 15 Apr 2008 13:31:57 -0700, Keith Thompson <ks*@cts.com>
wrote:
<snip>
>>Got it. Message-ID <87************@kvetch.smov.org>, available at
<http://groups.google.com/group/comp.lang.c/msg/02c5c1b781def71e>. It
was actually Ben Bacarisse who proposed the definition.
Thank you for finding it. I'd have owned up, but I was busy.

<snip>
I like Ben's definition, modulo corner cases. It captures what
people usually mean by globals.
The original intent was to propose a definition of the /term/ not the
/concept/ -- there would be no corner cases! Of course, it is not an
accident that it matches what people usually mean, but the idea was
that, by defining the term, it could be used here without causing a
thread to explode.

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

--
Ben.
Jun 27 '08 #42

P: n/a
On Apr 15, 8:20 pm, Ian Collins <ian-n...@hotmail.comwrote:
istillsh...@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()".

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

--
Ian Collins.
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.
Jun 27 '08 #43

P: n/a
is*********@gmail.com wrote:
On Apr 15, 8:20 pm, Ian Collins <ian-n...@hotmail.comwrote:
>Place the function declaration in a shared header. That way, you can
expose the value of silent and restrict write access to the file where
you define it.
*Please* don't quote signatures.
>
Suppose I have 100 functions in my program, is it a good idea to put
all their declarations in a header file called proto.h?
Not really, why would you? If all of those functions form a public
interface, you might want to exposes them. Mode likely...
Another way maybe putting them into separate .h files, i.e., putting
all function declarations defined in a.c into a.h, all function
declarations defined in b.c into b.h, and so on.
....you would want to group your non-static functions in a number of headers.
But including a main.h (containing getSilent()) for every .c is not a
good idea.
Why not? The name's a bit naff, but putting all your debug related
declarations in something like "debug.h" and including it where required
is perfectly sound practice.

--
Ian Collins.
Jun 27 '08 #44

P: n/a
is*********@gmail.com writes:
On Apr 15, 8:20 pm, Ian Collins <ian-n...@hotmail.comwrote:
>istillsh...@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()".

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

--
Ian Collins.

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.

--
Ben.
Jun 27 '08 #45

P: n/a
On Apr 15, 9:57 pm, Ian Collins <ian-n...@hotmail.comwrote:
But including a main.h (containing getSilent()) for every .c is not a
good idea.

Why not? The name's a bit naff, but putting all your debug related
declarations in something like "debug.h" and including it where required
is perfectly sound practice.
If I only want to use getSilent() defined in main.c, can I just write
a line on top of other.c,

int getSilent()

to let compilation pass?

This way, I don't need to create main.h just for using getSilent()
Jun 27 '08 #46

P: n/a
is*********@gmail.com wrote:
On Apr 15, 9:57 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>But including a main.h (containing getSilent()) for every .c is not a
good idea.
Why not? The name's a bit naff, but putting all your debug related
declarations in something like "debug.h" and including it where required
is perfectly sound practice.

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

int getSilent()

to let compilation pass?

This way, I don't need to create main.h just for using getSilent()
You could, but what's wrong with a header?

--
Ian Collins.
Jun 27 '08 #47

P: n/a
On Apr 15, 10:25 pm, Ian Collins <ian-n...@hotmail.comwrote:
istillsh...@gmail.com wrote:
On Apr 15, 9:57 pm, Ian Collins <ian-n...@hotmail.comwrote:
>But including a main.h (containing getSilent()) for every .c is not a
good idea.
Why not? The name's a bit naff, but putting all your debug related
declarations in something like "debug.h" and including it where required
is perfectly sound practice.
If I only want to use getSilent() defined in main.c, can I just write
a line on top of other.c,
int getSilent()
to let compilation pass?
This way, I don't need to create main.h just for using getSilent()

You could, but what's wrong with a header?
Nothing wrong. Thanks.
Jun 27 '08 #48

P: n/a
is*********@gmail.com said:
I have several further questions on global variables.

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

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

P: n/a
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?
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
Jun 27 '08 #50

112 Replies

This discussion thread is closed

Replies have been disabled for this discussion.