Using extern

Discussion in 'C Programming' started by Praveen, Feb 26, 2007.

  1. Praveen

    Praveen Guest

    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
    Praveen, Feb 26, 2007
    #1
    1. Advertising

  2. Praveen

    Chris Dollin Guest

    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.
    Chris Dollin, Feb 26, 2007
    #2
    1. Advertising

  3. Praveen

    santosh Guest

    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.
    santosh, Feb 26, 2007
    #3
  4. Praveen

    Guest

    On 26 Feb, 11:41, "Praveen" <> wrote:
    > 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, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jakob Bieling

    Q: Using an extern const size_t?

    Jakob Bieling, Feb 28, 2004, in forum: C++
    Replies:
    5
    Views:
    548
    Jakob Bieling
    Feb 28, 2004
  2. Christopher M. Lusardi
    Replies:
    3
    Views:
    514
    Christopher M. Lusardi
    May 15, 2004
  3. Thomas Matthews
    Replies:
    5
    Views:
    2,373
    tom_usenet
    Aug 2, 2004
  4. Replies:
    4
    Views:
    304
    Keith Thompson
    Jul 19, 2006
  5. Andre
    Replies:
    5
    Views:
    520
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page