Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Variable scope

Reply
Thread Tools

Re: Variable scope

 
 
Keith Thompson
Guest
Posts: n/a
 
      06-24-2009
mattia <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
 
Morris Keesan
Guest
Posts: n/a
 
      06-24-2009
On Wed, 24 Jun 2009 16:37:22 -0400, mattia <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
jameskuyper
Guest
Posts: n/a
 
      06-24-2009
Morris Keesan wrote:
> On Wed, 24 Jun 2009 16:37:22 -0400, mattia <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments