How to eliminate this global variable, silent?

I

istillshine

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?
 
B

Barry Schwarz

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
 
I

Ian Collins

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

Pradeep

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
 
I

istillshine

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 */
 
I

istillshine

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

Ben Bacarisse

Pradeep said:
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.
 
K

Keith Thompson

Ben Bacarisse said:
[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:

Q> I've heard that global variables are a bad idea, but I think
Q> they're really cool.

A> Assuming that "global variables" refers to "objects defined at file
A> scope 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).
 
R

Richard

Keith Thompson said:
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".
 
L

lawrence.jones

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

Richard

"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. But I tire of the usual suspects in this NG
playing games with language in order to show off their knowledge of
standard. Everyone in the programming world knows what is meant by a
global variable. And Heathfield's claim that C doesn't have them is
total and utter hogwash.
local variable, but it's not exactly a global variable, either.

The scope of the variable is local. The fact that its not an automatic
variable is another issue.
 
B

Ben Bacarisse

"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.
 
B

Ben Bacarisse

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

Nick Keighley

No. Because its file scope. Not global scope. I don think I'm being
overly simplistic here. But I tire of the usual suspects in this NG
playing games with language in order to show off their knowledge of
standard. Everyone in the programming world knows what is meant by a
global variable.

I have encountered people who referred to file scope objects as
"global parameters" or "globals".

So when I hear the term "global" I'm liable to ask "how global?".
 
R

Richard

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

Keith Thompson

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

Keith Thompson

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

Richard Tobin

C has global variables and no amount of silly word games will change
that.
[/QUOTE]
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
 
R

Richard

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

Richard

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[/QUOTE]

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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top