1

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!

jeevan
  • 247
  • 5
  • 12
  • 1
    FYI, `static` works the same for functions and global variables, so it's not as inconsistent as you think: http://stackoverflow.com/q/1856599/50079 – Jon Sep 01 '14 at 09:32
  • static function and variable both have at only in file scope. – Jayesh Bhoi Sep 01 '14 at 09:33
  • I think the gist of his question is why they used the same keyword for giving global variables/functions file scope as the keyword for making local variables keep their value across calls. – Barmar Sep 01 '14 at 09:34
  • For both object or functions, the `static` keywords specifies the linkage of the function. An object or a function declared with the `static` specifier has *internal linkage*. – ouah Sep 01 '14 at 09:35
  • Also By default any function that is defined in a C file is extern.So you can avoid this by `static`. – Jayesh Bhoi Sep 01 '14 at 09:36
  • Yes exactly @Barmar and by the way it's not "his", I am a woman! – jeevan Sep 01 '14 at 09:43
  • `where in the memory a static function would get stored.."` You can see here http://stackoverflow.com/questions/93039/where-are-static-variables-stored-in-c-c – Jayesh Bhoi Sep 01 '14 at 09:44
  • @jeevan Sorry about that. http://en.wikipedia.org/wiki/On_the_Internet,_nobody_knows_you're_a_dog – Barmar Sep 01 '14 at 09:45
  • What is your point Mr.? @Barmar – jeevan Sep 01 '14 at 09:47
  • It's the Internet, you can't generally tell someone's gender, so I habitually use "him". I didn't notice your avatar. – Barmar Sep 01 '14 at 09:51
  • possible duplicate of [What does "static" mean in a C program?](http://stackoverflow.com/questions/572547/what-does-static-mean-in-a-c-program) – n0p Sep 01 '14 at 13:47

3 Answers3

2

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Yes thank you, At first I wondered how come same keyword does two things, but now I know it is overloaded. "This keyword is overloaded for variables", can you be more specific about this point please! Like if you have any links about this topic how its overloaded and things, please give me! – jeevan Sep 01 '14 at 09:42
  • Thank you, "All variables which do not have dynamic storage duration, do not have thread storage duration", this helped me a lot! – jeevan Sep 01 '14 at 09:55
  • @eevan I am sorry. I forgot to mention a third meaning of the keyword in C++. See my updated post. – Vlad from Moscow Sep 01 '14 at 09:58
2

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.

bluefoggy
  • 961
  • 1
  • 9
  • 23
1

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.

user694733
  • 15,208
  • 2
  • 42
  • 68