32

I wonder about the use of the static keyword as scope limiting for variables in a file, in C.

The standard way to build a C program as I see it is to:

  • have a bunch of c files defining functions and variables, possibly scope limited with static.
  • have a bunch of h files declaring the functions and possibly variables of the corresponding c file, for other c files to use. Private functions and variables are not published in the h file.
  • every c file is compiled separately to an o file.
  • all o files are linked together to an application file.

I see two reasons for declaring a gobal as static, if the variable is not published in the h file anyway:

  • one is for readability. Inform future readers including myself that a variable is not accessed in any other file.
  • the second is to prevent another c file from redeclaring the variable as extern. I suppose that the linker would dislike a variable being both extern and static. (I dislike the idea of a file redeclaring a variable owned by someone else as extern, is it ok practice?)

Any other reason?

Same goes for static functions. If the prototype is not published in the h file, other files may not use the function anyway, so why define it static at all? I can see the same two reasons, but no more.

1
  • Six years later, I now know that static does not limit scope, it gives internal linkage. It is similar, if you'd consider a translation unit as a scope, but not the correct term.
    – Gauthier
    Mar 3, 2016 at 12:41

6 Answers 6

34

When you talk about informing other readers, consider the compiler itself as a reader. If a variable is declared static, that can affect the degree to which optimizations kick in.

Redefining a static variable as extern is impossible, but the compiler will (as usual) give you enough rope to hang yourself.

If I write static int foo; in one file and int foo; in another, they are considered different variables, despite having the same name and type - the compiler will not complain but you will probably get very confused later trying to read and/or debug the code. (If I write extern int foo; in the second case, that will fail to link unless I declare a non-static int foo; somewhere else.)

Global variables rarely appear in header files, but when they do they should be declared extern. If not, depending on your compiler, you risk that every source file which includes that header will declare its own copy of the variable: at best this will cause a link failure (multiply-defined symbol) and at worst several confusing cases of overshadowing.

1
  • 1
    Interesting. I understand that this "extern global in header file" still needs definition in a c file, since extern in the h file makes it a mere declaration.
    – Gauthier
    Jun 4, 2010 at 12:46
10

By declaring a variable static on file level (static within function has a different meaning) you forbid other units to access it, e.g. if you try to the variable use inside another unit (declared with extern), linker won't find this symbol.

10
  • This is exactly one of the two reasons I mentioned: "the second is to prevent another c file from using the seemingly scope-limited global by redeclaring it as extern." And I wondered about the quality of such practice?
    – Gauthier
    Jun 4, 2010 at 12:10
  • @Gaunthier So your post has an internal conflict then
    – qrdl
    Jun 4, 2010 at 12:16
  • 2
    IMO, static has the same meaning in all three well-known uses (file variable, function variable, function) and that is: "this has limited scope and has a constant address throughout the program execution".
    – Gauthier
    Jun 4, 2010 at 12:17
  • 1
    Well, in this sense meaning is the same, but purpose is different - static on file level controls visibility, static on function level controls persistence.
    – qrdl
    Jun 4, 2010 at 12:19
  • If you declare something static in one compilation unit, you cannot redeclare it as extern in another compilation unit. They will be seen as different things. static serves also to prevent namespace pollution - e.g. you can have 2 compilation units contain a function named sort , and they will be treated as different things.
    – nos
    Jun 4, 2010 at 12:24
9

When you declare a static function the call to the function is a "near call" and in theory it performs better than a "far call". You can google for more information. This is what I found with a simple google search.

2
  • Also interesting, although platform dependent (my target has only one CALL instruction to absolute, and no far/near).
    – Gauthier
    Jun 4, 2010 at 13:14
  • e.g. gcc can switch calling convention(to something faster) for static functions on many platforms.
    – nos
    Jun 4, 2010 at 17:00
2

If a global variable is declared static, the compiler can sometimes make better optimizations than if it were not. Because the compiler knows that the variable cannot be accessed from other source files, it can make better deductions about what your code is doing (such as "this function does not modify this variable"), which can sometimes cause it to generate faster code. Very few compilers/linkers can make these sorts of optimizations across different translation units.

1

If you declare a variable foo in file a.c without making it static, and a variable foo in file b.c without making it static, both are automatically extern which means the linker may complain if you initialise both, and assign the same memory location if it doesn't complain. Expect fun debugging your code.

If you write a function foo () in file a.c without making it static, and a function foo () in file b.c without making it static, the linker may complain, but if it doesn't, all calls to foo () will call the same function. Expect fun debugging your code.

-1

My favorite usage of static is being able to store methods that I wont have to Inject or create an object to use, the way I see it is, Private Static Methods are always useful, where public static you have to put some more time in thinking of what it is your doing to avoid what crazyscot defined as, getting your self too much rope and accidentally hanging ones self!

I like to keep a folder for Helper classes for most of my projects that mainly consist of static methods to do things quickly and efficiently on the fly, no objects needed!

1
  • 2
    Question is about C, not C++. In C there are no classes, no methods, and no private members. Moreover, in C++ there is a difference in static functions and static methods. Jun 4, 2010 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.