tags:

views:

3864

answers:

8

I've seen the word static used in different places in C code; is this like a static function/class in C# (where the implementation is shared across objects)?

+42  A: 
  1. A static variable inside a function keeps its value between invocations.
  2. A static global variable or a function is "seen" only in the file it's declared in

(1) is the more foreign topic if you're a newbie, so here's an example:

#include <stdio.h>

void foo()
{
    int a = 10;
    static int sa = 10;

    a += 5;
    sa += 5;

    printf("a = %d, sa = %d\n", a, sa);
}


int main()
{
    int i;

    for (i = 0; i < 10; ++i)
        foo();
}

This prints:

a = 15, sa = 15
a = 15, sa = 20
a = 15, sa = 25
a = 15, sa = 30
a = 15, sa = 35
a = 15, sa = 40
a = 15, sa = 45
a = 15, sa = 50
a = 15, sa = 55
a = 15, sa = 60

This is useful for cases where a function needs to keep some state between invocations, and you don't want to use global variables. Beware, however, this feature should be used very sparingly - it makes your code not thread-safe and harder to understand.

(2) Is used widely as an "access control" feature. If you have a .c file implementing some functionality, it usually exposes only a few "public" functions to users. The rest of its functions should be made static, so that the user won't be able to access them. This is encapsulation, a good practice.

Quoting Wikipedia:

In the C programming language, static is used with global variables and functions to set their scope to the containing file. In local variables, static is used to store the variable in the statically allocated memory instead of the automatically allocated memory. While the language does not dictate the implementation of either type of memory, statically allocated memory is typically reserved in data segment of the program at compile time, while the automatically allocated memory is normally implemented as a transient call stack.

See here and here for more details.

And to answer your second question, it's not like in C#. In C++, however, static is used to "share" methods and data between objects of the same class. C has no classes, so no such feature.

Eli Bendersky
Being a little pedantic, it's compilation unit, not file.
paxdiablo
Pax, the OP doesn't know about static, so you suggest plunging him into the difference between compilation units and files ? :-)
Eli Bendersky
Wait, what's a compilation unit?
A compilation unit is a single file the compiler sees. Your .c file may include other .c files, but after the preprocessor sorting out the includes, the compiler eventually sees just a single "compilation unit".
Eli Bendersky
David, if you're new to C, this issue shouldn't bother you at all. It doesn't bother 99.9% of C programmers anyway.
Eli Bendersky
why not plunging him? it's best for them to know about the real stuff asap imho.
Johannes Schaub - litb
so if you define a static in one header file that you include into a file, but in that file define the same static another time, it is an error, because the two definitions appear in the same translation unit.
Johannes Schaub - litb
This has got me thinking. Does the compilation unit consist of only its *.c file. For example, if I have a file called foo.c and a header called foo.h. Would the 2 of them consist of 1 compilation unit? Or would the foo.c be one complete compilation unit without the foo.h?
robUK
@robUK: the compiler doesn't even know about the .h files - these are combined into the .c files in the pre-processor. So yes you can say that the .c file, with all the headers included into it, are a single compilation unit.
Eli Bendersky
+1  A: 

From Wikipedia:

In the C programming language, static is used with global variables and functions to set their scope to the containing file. In local variables, static is used to store the variable in the statically allocated memory instead of the automatically allocated memory. While the language does not dictate the implementation of either type of memory, statically allocated memory is typically reserved in data segment of the program at compile time, while the automatically allocated memory is normally implemented as a transient call stack.

OscarRyz
A: 

If you declare a variable in a function static, its value will not be stored on the function call stack and will still be available when you call the function again.

If you declare a global variable static, its scope will be restricted to within the file in which you declared it. This is slightly safer than a regular global which can be read and modified throughout your entire program.

Sam Hoice
A: 

static means different things in different contexts.

  1. You can declare a static variable in a C function. This variable is only visible in the function however it behaves like a global in that it is only initialized once and it retains its value. In this example, everytime you call foo() it will print an increasing number. The static variable is initialized only once.

    void foo () { static int i = 0; printf("%d", i); i++ }

  2. Another use of static is when you implement a function or global variable in a .c file but don't want it's symbol to be visible outside of the .obj generated by the file. e.g.

    static void foo() { ... }

m-sharp
+9  A: 

Short answer ... it depends.

  1. Static defined local variables do not loose their value between function calls. In other words they are global variables, but scoped to the local function they are defined in.

  2. Static global variables are not visible outside of the C file they are defined in.

  3. Static functions are not callable outside of the C file they are defined in.

Casey
+2  A: 

Depends:

int foo()
{
   static int x;
   return ++x;
}

Whould return 1,2,3.. and so on --- the variable is not on stack.

a.c:

static int foo()
{
}

Means that this function has scope only in this file. So if a.c and b.c can have different foo() and foo is not exposed to shared objects. So if you defined foo in a.c you can't access it from b.c or from any other places.

In most C libraries all "private" functions are static and most "public" are not.

Artyom
+1  A: 

In C, static has two meanings, depending on scope of its use. In the global scope, when an object is declared at the file level, it means that that object is only visible within that file.

At any other scope it declares an object that will retain its value between the different times that the particular scope is entered. For example, if an int is delcared within a procedure:

void procedure(void)
{
   static int i = 0;

   i++;
}

the value of 'i' is initialized to zero on the first call to the procedure, and the value is retained each subsequent time the procedure is called. if 'i' were printed it would output a sequence of 0, 1, 2, 3, ...

A: 

If you declare this in a mytest.c file:

static int my_variable;

Then this variable can only be seen from this file. The variable cannot be exported anywhere else.

If you declare inside a function the value of the variable will keep its value each time the function is called.

A static function cannot be exported from outside the file. So in a *.c file, you are hiding the functions and the variables if you declare them static.

robUK