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

Using extern

P: n/a


Hi,

I would like to know is it possible not to use extern in a program.
For eg I have the following programs

file a.c contains a variable declaration as follows
bool a = TRUE;

I have declared the variable a as extern in the header file a.h as
follows
extern bool a;

In another file b.c which has included the header file a.h I try to
set the variable to FALSE;

I would like to know is there a way of not to use the extern
declaration and still achieve the above scenario.

Thanks in advance

Feb 26 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Praveen wrote:
I would like to know is it possible not to use extern in a program.
For eg I have the following programs

file a.c contains a variable declaration as follows
bool a = TRUE;
(Where did your boolean type and value come from? If that `bool`
is C99, ten shouldn't `TRUE` be `true`?)
I have declared the variable a as extern in the header file a.h as
follows
extern bool a;

In another file b.c which has included the header file a.h I try to
set the variable to FALSE;
How do you do this? Did you suceeed?
I would like to know is there a way of not to use the extern
declaration and still achieve the above scenario.
Why on Earth would you not want to use the provided mechanism
for allowing a compilation unit to refer to variables defined
in a different unit?

What are you actually trying to /do/?

--
Chris "electric hedgehog" Dollin
The shortcuts are all full of people using them.

Feb 26 '07 #2

P: n/a
Praveen wrote:
Hi,

I would like to know is it possible not to use extern in a program.
For eg I have the following programs

file a.c contains a variable declaration as follows
bool a = TRUE;
If this is the bool defined in C99 then it can only be set to 'true'
or 'false'. All three macros, (yes bool is actually a macro) are
declared in stdbool.h.
I have declared the variable a as extern in the header file a.h as
follows
extern bool a;

In another file b.c which has included the header file a.h I try to
set the variable to FALSE;
Set it to false.
I would like to know is there a way of not to use the extern
declaration and still achieve the above scenario.

Thanks in advance
Why do you want to battle with a language? If you don't like the
mechanisms provided by C, then pick another language.

PS. By receiving a pointer to 'a' from the translation unit where it's
defined, it's possible to do what you want.

Feb 26 '07 #3

P: n/a
On 26 Feb, 11:41, "Praveen" <rpraveen1...@gmail.comwrote:
Hi,

I would like to know is it possible not to use extern in a program.
I assume you want to access data in separate compilation units,
without using extern to share the data.

The answer is "yes" of course. It depends whether you are happy with
the alternatives.

The two approaches which come to mind are either
a) provide a function in the "owning" module which returns the address
of the item you want to access
b) provide more controlled access using accessor functions - one to
get the value of the item, one or more others to set/increment/
decrement/... the value.

Feb 26 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.