tags:

views:

228

answers:

6

Is the static keyword in C used only for limiting the scope of a variable to a single file?

I need to know if I understood this right. Please assume the following 3 files,

file1.c

int a;

file2.c

int b;

file3.c

static int c;

Now, if the 3 files are compiled together, then the variables "a" & "b" should have a global scope and can be accessed from any of the 3 files. But, variable "c" being static, can only be accessed from file3.c, right?

Does static have any other use in C ? (other than to limit the scope of a variable as shown above?)

+5  A: 

static is also used within a function definition to define a variable which keeps its value between function calls. I found an example here. In contrast, variables which are created anew with each function call are called automatic.

Kinopiko
I wouldn't call it "opposite". It is like saying that red is opposite of yellow. In C89/90 there were two storage durations: static and automatic, but there was no "opposition" between them. C language (C99) has three storage durations: static, automatic and allocated. Again, there's no "opposite" here.
AndreyT
I've altered the answer slightly.
Kinopiko
+4  A: 

An example to augment Kinopiko’s answer:

#include <stdio.h>

int foo() {
    static int foo = 0;
    return ++foo;
}

int main() {
    printf("%i\n", foo()); // 1
    printf("%i\n", foo()); // 2
}

This can be used for example to return a safe pointer to a local function variable. Or in Objective-C it’s sometimes used to guard against repeated class initialization:

- (void) initialize
{
    static BOOL initialized = NO;
    if (initialized)
        return;
    // …perform initialization…
    initialized = YES;
}
zoul
In the example you just gave, let me assume it's in a file called "foo.c". Now if I have another file "aaa.c" and I call the function foo() from it, will the static variable still keep it's value? (I mean, if I call foo() from aaa.c, it's the same as calling it from another compilation unit, right?)
chronodekar
@chronodekar: what hapenned when you tried? Yes, calling `foo()` from aaa.c is the same as calling it from foo.c: the "internal" variable gets updated and the updated value is returned.
pmg
+2  A: 

You are correct, this is called "static linkage": The symbol declared as static is only available in the compilation unit where it is defined.

The other use of static would be inside a function:

void f() {
  static int counter = 0;
  counter++;
  // ...
}

In this case the variable is only initialized once and keeps it's value through different calls of that function, like it would be a global variable. In this example the counter variable counts the number of times the function was called.

sth
You might want to give _counter_ a type :-)
Mads Elvheim
Ooops... Added the type to counter :-)
sth
+3  A: 

The static keyword serves two distinct purposes in C, what I call duration and visibility.

When used at file level (outside of any function), it controls visibility. The duration of those variables are already defined as being the entire duration of the program so you don't need static for that.

Static variable at file level are invisible to anything outside the translation unit (the linker can't see it).

When used at function level (inside a function), it controls duration. That's because the visibility is already defined as being local to that function.

In that case, the duration of the variable is the entire duration of the program and the value is maintained between invocations of the function.

paxdiablo
What you called "visibility" is really called "linkage". Visibility is a completely different thing. "Visibility" is the same thing as "scope". `static` has no effect on visibility. `static` has no effect on scope.
AndreyT
Visibility is *not* the same thing as scope. The scope of a file-level variable ends at the end of the translation unit. It's visibility is available *outside* the translation unit when the linker can see it. I specifically used the word visibility to avoid the clash with scope.
paxdiablo
Visibility is more to do with the linkage, but in a term that's easier to explain to people than the dry language of the standard :-)
paxdiablo
Incorrect. You are inventing your own meaning for the term visibility. The term "visibility" is already taken in C language and it means the visibility of an identifier, i.e. its *scope* (defined in 6.2.1/2). What you are talking about is called *linkage* and it is never referred to as "visibility". Open the standard and read the definitions. Stop confusing people with made-up interpretations of stanadrd terms.
AndreyT
Yes, I am inventing my own term. If I'm teaching someone the *standard*, I'll happily use the terms in there. But when I'm just teaching students about the basics of C (and it appears chronodekar is at that level), I'll use terms that make more sense.
paxdiablo
Most of the confusion here stems from the "inventors of their own terms". No, you don't get to invent your own terms here. If you still want to, you'll have to choose the "unused" words for your own terms and you will have to provide precise definitions for these terms. As for the already-standardized terms: again, no, you will not [re-]invent them, regardless of how much you want to. And "visibility" and "scope" are already taken, sorry.
AndreyT
... Which is especially important when it comes to those who just begin to learn the language. There's no greater disservice to them than to "plant" the incorrect self-invented definitions of universally accepted terms into their heads.
AndreyT
What an absolute crock. Do you really believe that all those terms in the standard make things *clearer* for a newbie? Talk of static storage duration and external linkage is not going to help them out at this point. Fine, once they learn the language well, they can look into the esoteric bits, but not when they're just starting. Again, if I'm talking to a "language lawyer" or someone that knows the standard very well, I'm happy using the real terms. But I'm not doing any learner a disservice by explaining the basic concepts in a more understandable way. You need to decide your audience.
paxdiablo
I'm talking about the term "visibility". This is the term you were trying to hijack. The term "visibility", as defined in the standard, has absolutely natural and very useful meaning. There's absolutely nothing unclear about it. By hijacking and redefining this term to something *completely unrelated* you are only creating confusion, not clearing it up.
AndreyT
It's your right to complain if you want @Andrey. But I'll still target my audience with appropriate terms. If you're a compiler writer or someone examining esoteric stuff like "are the aplha characters contiguous codepoints?" then, by all means look at the standard. If you just want to know how static works, you don't need to - there are simpler ways to explain it. I'm probably not going to convince you and I'm very certain you won't change my mind, so it's probably best to leave it there.
paxdiablo
+1  A: 

You are misusing the term "scope". static in C has absolutely nothing to do with scope.

Scope is the region where the name of an entity (variable, function, typename etc.) is visible. In C language "file scope" is the largest scope ever. For that reason, there's no point in limiting anything to a single file: there's simply nothing larger to limit. There's no such thing as "global scope" in C. The term "global scope" is sometimes used informally, but in that case it has the same meaning as "file scope".

Again, static in C has absolutely nothing to do with scope. static in C affects storage duration of an object and linkage of an identifier. When used with objects (variables) static gives the object static storage duration (i.e. the object exists as long as the program runs). And, when used with identifiers of non-local objects or functions, it gives them internal linkage, meaning that the same identifier refers to the same entity within a single translation unit (where the entity is defined), but not in other translation units.

AndreyT
+1  A: 

A variable may have three kinds of storage:

  1. In program's Static Area
  2. On stack (during function call)
  3. On Heap (when you allocate using new/malloc)

Global variables are always stored in static area. But to store a local variable in static area, you need the keyword static. As a static variable is not allocated on stack, you can access the variable on subsequent calls.
Also static keyword at global scope gives a variable internal linkage.Consequently the variable cannot be accessed from some other file using the extern qualifier.

Neeraj