|
Home > Archive > Unix Programming > December 2006 > static char ?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| I often see 'static char' defined as a global in some header file. Why
is that? As far as i understand it, for example, a static int inside a
function retains the value even when the function exits. But seeing
that a global variable already has a scope of the whole program, it
seems a bit redundant to define a 'static char' as a global?
| |
| Pascal Bourguignon 2006-12-18, 1:20 pm |
| atv <alef@xs4all.nl> writes:
> I often see 'static char' defined as a global in some header file. Why
> is that? As far as i understand it, for example, a static int inside a
> function retains the value even when the function exits. But seeing
> that a global variable already has a scope of the whole program, it
> seems a bit redundant to define a 'static char' as a global?
static global variables have file scope.
non static global variable have program scope.
So, declaring a global variable with static allows you to define one
variable per file using the same name.
--
__Pascal Bourguignon__ http://www.informatimago.com/
"Our users will know fear and cower before our software! Ship it!
Ship it and let them flee like the dogs they are!"
| |
| Wayne C. Morris 2006-12-18, 7:22 pm |
| In article <4586dca7$0$326$e4fe514c@news.xs4all.nl>, atv <alef@xs4all.nl>
wrote:
> I often see 'static char' defined as a global in some header file. Why
> is that? As far as i understand it, for example, a static int inside a
> function retains the value even when the function exits. But seeing
> that a global variable already has a scope of the whole program, it
> seems a bit redundant to define a 'static char' as a global?
The meaning of the 'static' keyword depends on whether the variable
definition is inside a function.
Inside a function, it causes the variable to retain its value between calls
to that function.
Outside any function, it causes the variable to be accessible only to
functions in that .C source file.
If it's in a .H header file, each .C file that includes that header file
will get a separate copy of that variable. The variable does NOT have
global scope throughout the whole program, because it's not one variable.
| |
|
| On 2006-12-18 20:44:25 +0100, "Wayne C. Morris"
<wayne.morris@this.is.invalid> said:
> In article <4586dca7$0$326$e4fe514c@news.xs4all.nl>, atv
> <alef@xs4all.nl> wrote:
>
>
> The meaning of the 'static' keyword depends on whether the variable
> definition is inside a function.
>
> Inside a function, it causes the variable to retain its value between
> calls to that function.
>
> Outside any function, it causes the variable to be accessible only to
> functions in that .C source file.
>
> If it's in a .H header file, each .C file that includes that header
> file will get a separate copy of that variable. The variable does NOT
> have global scope throughout the whole program, because it's not one
> variable.
Thanks everyone. I find this concept hard to understand i must admit. I
will do some more research on this :-)
| |
| Pascal Bourguignon 2006-12-18, 7:22 pm |
| atv <alef@xs4all.nl> writes:
>
>
> Thanks everyone. I find this concept hard to understand i must
> admit. I will do some more research on this :-)
Yes. Perhaps reading some C reference manual, or just a C programming
tutorial.
--
__Pascal Bourguignon__ http://www.informatimago.com/
Grace personified,
I leap into the window.
I meant to do that.
| |
| Barry Margolin 2006-12-19, 1:31 am |
| In article <45872fae$0$325$e4fe514c@news.xs4all.nl>,
atv <alef@xs4all.nl> wrote:
> Thanks everyone. I find this concept hard to understand i must admit. I
> will do some more research on this :-)
Don't feel too bad. The language designers' desire to keep the language
terse and minimize the number of keywords resulted in overloading words
like this in confusing ways.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Maxim Yegorushkin 2006-12-20, 7:24 am |
|
Pascal Bourguignon wrote:
> atv <alef@xs4all.nl> writes:
>
>
> static global variables have file scope.
> non static global variable have program scope.
If it is c++, global const variables also have internal linkage, as if
were declared static.
|
|
|
|
|