jeevan
jeevan

Reputation: 255

What is the exact reason for the keyword static working differently for variables and functions

If we use static in-front of a variable, it's value remain intact for the entire cycle of the program's execution in between function calls. But if we use static with functions they become local to the file in which they are declared. I know this is the way, but I want to know the reason exactly why? Why does static behave in two ways? I tried net but no luck, please explain me! Also please tell me where in the memory a static function would get stored, I personally thinks it is in stack!

Upvotes: 2

Views: 198

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

In fact keyword static has the same meaning for functions and variables when it is used as the specifier of the linkage that is functions and variables in namespaces declared with keyword static have internal linkage.

From the C++ Standard (3.5 Program and linkage)

3 A name having namespace scope (3.3.6) has internal linkage if it is the name of — a variable, function or function template that is explicitly declared static

Static functions are stored the same way as other functions except that their names are not exported as external names.

This keyword is overloaded for variables. it also denotes static storage duration. It is what you are speaking about in your post.

From the C++ Standard (3.7.1 Static storage duration)

1 All variables which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration. The storage for these entities shall last for the duration of the program (3.6.2, 3.6.3).

3 The keyword static can be used to declare a local variable with static storage duration.

4 The keyword static applied to a class data member in a class definition gives the data member static storage duration.

There is a third meaning of the keyword static in C++ relative to members of a class (in C there is no classes so this is not valid for C).

1 A data or function member of a class may be declared static in a class definition, in which case it is a static member of the class.

Upvotes: 3

user694733
user694733

Reputation: 16047

In C static means:

  • Internal linkage. Object is not visible to other compilation units (= other .c files).
  • Object exists to the end of program.

It doesn't matter if object is variable or function.

Note that scope of static variable can be limited.

static void A(void);
static int B;

void test(void) {
    static int C = 0;
    // A, B and C visible
}
void test2(void) {
    // Only A and B visible
}

Because static variables have to live long they are typically allocated in the beginning of the program, so they don't necessarily exist on stack.

Upvotes: 1

bluefoggy
bluefoggy

Reputation: 1061

I don't have enough reputation to comment on it. So writing it here.

Quoting few lines from the book "Expert C Programming - Peter van der Linden" might answer your question.

One problem is that C is so terse. Just adding, changing, or omitting a single character often gives you a program that is still valid but does something entirely different. Worse than that, many symbols are "overloaded"—given different meanings when used in different contexts. Even some keywords are overloaded with several meanings, which is the main reason that C scope rules are not intuitively clear to programmers.

static : Inside a function, retains its value between calls ; At the function level, visible only in this file

extern : Applied to a function definition, has global scope (and is redundant) ; Applied to a variable, defined elsewhere

So what i meant to say is these are some pitfalls of the language. Not sure if this helps.

Upvotes: 2

Related Questions