static global vars and functions 
Author Message
 static global vars and functions

Hello all,

Could anyone tell how 'static' keyword affects _global_ variables
and functions ?
E.g.:

int  somevar;
static int  othervar;

int somefunc() { ... }
static int otherfunc() { ... }

What is the difference between the cases when I use 'static' and when I
don't ?

Thanks.



Mon, 08 Sep 2003 19:53:30 GMT  
 static global vars and functions

Quote:
> Could anyone tell how 'static' keyword affects _global_ variables
> and functions ?
> E.g.:

> int  somevar;
> static int  othervar;

> int somefunc() { ... }
> static int otherfunc() { ... }

> What is the difference between the cases when I use 'static' and when I
> don't ?

Shortly: using "static" in this contexts means that this function/variable
is for internal use only (of the file) and is not to be called/used from
an another file.

I suggest you to read the FAQ

--
Eli Bendersky



Mon, 08 Sep 2003 20:14:51 GMT  
 static global vars and functions

Quote:
> Could anyone tell how 'static' keyword affects _global_ variables
> and functions ?

You mean file scope, I suppose? There is no single thing in C that
corresponds to all uses of "global"; in particular, the keyword you're
asking about can turn variables from one type of "global" into another.

Quote:
> int  somevar;
> static int  othervar;

> int somefunc() { ... }
> static int otherfunc() { ... }

> What is the difference between the cases when I use 'static' and when I
> don't ?

It gives them internal linkage. That is, functions from the same
translation unit (roughly, the same file) can use it directly (and it's
still "global" in the sense that all of them refer to the same object),
but functions from other translation units can't (and it is thus not
"global" in the sense of world-readable), though they can still be
passed a pointer to it.

Richard



Mon, 08 Sep 2003 20:22:00 GMT  
 static global vars and functions
On Thu, 22 Mar 2001 14:53:30 +0300, "Aleksey A. Perov"

Quote:

>Hello all,

>Could anyone tell how 'static' keyword affects _global_ variables
>and functions ?

When you put the static keyword on a variable defined at file scope, it
effectively becomes private to that file, e.g. you cannot extern it for use
in other files. Same for functions, as static function cannot be extern'd
for use in other files.

Quote:
>E.g.:

>int  somevar;

Can be extern'd and is considered to be a global variable but it doesn't
have to be if it is never extern'd.

Quote:
>static int  othervar;

Cannot be extern'd and thus cannot be considered a global variable. It can
be used by any function below its definition in the file.

Quote:
>int somefunc() { ... }

Similar to int somevar

Quote:
>static int otherfunc() { ... }

Similar to int othervar

Quote:
>What is the difference between the cases when I use 'static' and when I
>don't ?

Answers given thus far should explain it.
--
- Mark
- Embedded Firmware Design, Inc.
- http://www.embeddedfw.com


Mon, 08 Sep 2003 23:05:06 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. global/static vars inside loops

2. global/static vars inside loops

3. global vars vs: local vars

4. how to access local and global vars in a function

5. suggestions on how to fire events invoking the Fire_ConnectEv(LONG result_code) function from global (static) functions

6. const global vars

7. Global vars versus pointers

8. global vs local vars

9. global Vars

10. Global vs. Local vars and .MAP

11. Accessing vars in global and module scope in ANSI C

12. Accessing vars in global and module scope in ANSI C

 

 
Powered by phpBB® Forum Software