Re: Variable scope

Discussion in 'C Programming' started by Keith Thompson, Jun 24, 2009.

  1. mattia <> writes:
    > Imagine I have an header file containing a variable declared static. I
    > include this header in n different files. Can you explain me the scope of
    > the static function and what can be good for?


    Which is it, a variable or a function? Is it a declaration or a
    definition? (If you don't know the difference, show us a concrete
    example.)

    --
    Keith Thompson (The_Other_Keith) <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"
     
    Keith Thompson, Jun 24, 2009
    #1
    1. Advertising

  2. On Wed, 24 Jun 2009 16:37:22 -0400, mattia <> wrote:

    I have an header file state_mng.h where
    > there is a definition like:
    > static unsigned long My_state; - this header is included in different
    > files. Imagine file_1.c and file_2.c include state_mng.h and they both
    > use My_state for assignment/evaluation. I wanted to know the scope of
    > My_state.


    file_1.c:My_state and file_2.c:My_state are two different unsigned long
    variables. Each variable is accessible to all code contained in the
    ..c file which includes state_mng.h, but not to any other code (and, of
    course, the declaration of the file-static variable could be hidden by
    any declarations of variables with the same name in functions in those
    source files). For example, modifying My_state in file_1.c has no effect
    on any code in file_2.c
     
    Morris Keesan, Jun 24, 2009
    #2
    1. Advertising

  3. Keith Thompson

    jameskuyper Guest

    Morris Keesan wrote:
    > On Wed, 24 Jun 2009 16:37:22 -0400, mattia <> wrote:
    >
    > I have an header file state_mng.h where
    > > there is a definition like:
    > > static unsigned long My_state; - this header is included in different
    > > files. Imagine file_1.c and file_2.c include state_mng.h and they both
    > > use My_state for assignment/evaluation. I wanted to know the scope of
    > > My_state.

    >
    > file_1.c:My_state and file_2.c:My_state are two different unsigned long
    > variables. Each variable is accessible to all code contained in the
    > .c file which includes state_mng.h, but not to any other code


    More precisely, it is accessible _by name_ in only one translation
    unit. It can be accessed from any translation unit by use of a
    pointer; but somewhere along the line there has to be an expression
    like &My_state, and the translation unit that contains that expression
    determines which My_state variable the pointer refers to.
     
    jameskuyper, Jun 24, 2009
    #3
    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. Paul Opal
    Replies:
    12
    Views:
    973
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    682
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    503
  4. David Filmer
    Replies:
    19
    Views:
    256
    Kevin Collins
    May 21, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    205
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page