Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal static variables in C, would you use them?

Tags:

In C you can have external static variables that are viewable every where in the file, while internal static variables are only visible in the function but is persistent

For example:

#include <stdio.h>  void foo_bar( void ) {         static counter = 0;         printf("counter is %d\n", counter);         counter++; } int main( void ) {         foo_bar();         foo_bar();         foo_bar();  return 0; } 

the output will be

counter is 0 counter is 1 counter is 2 

My question is why would you use an internal static variable? If you don't want your static variable visible in the rest of the file shouldn't the function really be in its own file then?

like image 532
hhafez Avatar asked Feb 10 '09 23:02

hhafez


People also ask

What is static variable when should it be used in C?

Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.

When should static variables be used?

Static variables, by contrast, are variables associated with a class itself, rather than a particular instance of that class. Static variables are used to keep track of information that relates logically to an entire class, as opposed to information that varies from instance to instance.

What is the difference between the internal static variable and the external static variable in C?

Internal static variables are active(visibility) in the particular function. External Static variables are active(visibility)throughout the entire program. Internal static variables are alive(lifetime) until the end of the function. External static variables are alive(lifetime) in the entire program.

What is the effect of making an internal local variable static?

Using the static keyword on a local variable changes its duration from automatic duration to static duration . This means the variable is now created at the start of the program, and destroyed at the end of the program (just like a global variable).


1 Answers

This confusion usually comes about because the static keyword serves two purposes.

When used at file level, it controls the visibility of its object outside the compilation unit, not the duration of the object (visibility and duration are layman's terms I use during educational sessions, the ISO standard uses different terms which you may want to learn eventually, but I've found they confuse most beginning students).

Objects created at file level already have their duration decided by virtue of the fact that they're at file level. The static keyword then just makes them invisible to the linker.

When used inside functions, it controls duration, not visibility. Visibility is already decided since it's inside the function - it can't be seen outside the function. The static keyword in this case, causes the object to be created at the same time as file level objects.

Note that, technically, a function level static may not necessarily come into existence until the function is first called (and that may make sense for C++ with its constructors) but every C implementation I've ever used creates its function level statics at the same time as file level objects.

Also, whilst I'm using the word "object", I don't mean it in the sense of C++ objects (since this is a C question). It's just because static can apply to variables or functions at file level and I need an all-encompassing word to describe that.

Function level statics are still used quite a bit - they can cause trouble in multi-threaded programs if that's not catered for but, provided you know what you're doing (or you're not threading), they're the best way to preserve state across multiple function calls while still providing for encapsulation.

Even with threading, there are tricks you can do in the function (such as allocation of thread specific data within the function) to make it workable without exposing the function internals unnecessarily.

The only other choices I can think of are global variables and passing a "state variable" to the function each time.

In both these cases, you expose the inner workings of the function to its clients and make the function dependent on the good behavior of the client (always a risky assumption).

like image 68
paxdiablo Avatar answered Oct 13 '22 04:10

paxdiablo