By Slava V


2009-02-17 18:26:02 8 Comments

The question was about plain functions, not static methods, as clarified in comments.

I understand what a static variable is, but what is a static function?

And why is it that if I declare a function, let's say void print_matrix, in let's say a.c (WITHOUT a.h) and include "a.c" - I get "[email protected]@....) already defined in a.obj", BUT if I declare it as static void print_matrix then it compiles?

UPDATE Just to clear things up - I know that including .c is bad, as many of you pointed out. I just do it to temporarily clear space in main.c until I have a better idea of how to group all those functions into proper .h and .c files. Just a temporary, quick solution.

12 comments

@RobertS supports Monica Cellio 2020-06-08 12:44:53

"What is a “static” function in C?"

Let's start at the beginning.

It´s all based upon a thing called "linkage":

"An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage. 29)There are three kinds of linkage: external, internal, and none."

Source: C18, 6.2.2/1


"In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity."

Source: C18, 6.2.2/2


If a function is defined without a storage-class specifier, the function has external linkage by default:

"If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern."

Source: C18, 6.2.2/5

That means that - if your program is contained of several translation units/source files (.c or .cpp) - the function is visible in all translation units/source files your program has.

This can be a problem in some cases. What if you want to use f.e. two different function (definitions), but with the same function name in two different contexts (actually the file-context).

In C and C++, the static storage-class qualifier applied to a function at file scope (not a static member function of a class in C++ or a function within another block) now comes to help and signifies that the respective function is only visible inside of the translation unit/source file it was defined in and not in the other TLUs/files.

"If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage. 30)"


30) A function declaration can contain the storage-class specifierstaticonly if it is at file scope; see 6.7.1.

Source: C18, 6.2.2/3


Thus, A static function only makes sense, iff:

  1. Your program is contained of several translation units/source files (.c or .cpp).

    and

  2. You want to limit the scope of a function to the file, in which the specific function is defined.

If not both of these requirements match, you don't need to wrap your head around about qualifying a function as static.


Side Notes:

  • As already mentioned, A static function has absolutely no difference at all between C and C++, as this is a feature C++ inherited from C.

    It does not matter that in the C++ community, there is a heartbreaking debate about the depreciation of qualifying functions as static in comparison to the use of unnamed namespaces instead, first initialized by a misplaced paragraph in the C++03 standard, declaring the use of static functions as deprecated which soon was revised by the committee itself and removed in C++11.

    This was subject to various SO questions:

    Unnamed/anonymous namespaces vs. static functions

    Superiority of unnamed namespace over static?

    Why an unnamed namespace is a "superior" alternative to static?

    Deprecation of the static keyword... no more?

    In fact, it is not deprecated per C++ standard yet. Thus, the use of static functions is still legit. Even if unnamed namespaces have advantages, the discussion about using or not using static functions in C++ is subject to one´s one mind (opinion-based) and with that not suitable for this website.

@PoJyun Chiou 2020-05-08 08:08:43

Since static function is only visible in this file. Actually, compiler can do some optimization for you if you declare "static" to some function.

Here is a simple example.

main.c

#include <stdio.h>

static void test() 
{
    ghost(); // This is an unexist function.
}

int main()
{
    int ret = 0;

#ifdef TEST
#else
    test();
#endif
    return (ret);
} 

And compile with

gcc -o main main.c

You will see it failed. Because you even not implement ghost() function.

But what if we use following command.

gcc -DTEST -O2 -o main main.c

It success, and this program can be execute normally.

Why? There are 3 key points.

  1. -O2 : Compiler optimization level at least 2.
  2. -DTEST : Define TEST, so test() will not be called.
  3. Defined "static" to test().

Only if these 3 conditions are all true, you can pass compilation. Because of this "static" declaration, compiler can confirm that test() will NEVER be called in other file. Your compiler can remove test() when compiling. Since we don't need test(), it does not matter whether ghost() is defined or implemented.

@Ciro Santilli 郝海东冠状病六四事件法轮功 2015-05-19 07:56:24

Minimal runnable multi-file scope example

Here I illustrate how static affects the scope of function definitions across multiple files.

a.c

#include <stdio.h>

/* Undefined behavior: already defined in main.
 * Binutils 2.24 gives an error and refuses to link.
 * https://stackoverflow.com/questions/27667277/why-does-borland-compile-with-multiple-definitions-of-same-object-in-different-c
 */
/*void f() { puts("a f"); }*/

/* OK: only declared, not defined. Will use the one in main. */
void f(void);

/* OK: only visible to this file. */
static void sf() { puts("a sf"); }

void a() {
    f();
    sf();
}

main.c

#include <stdio.h>

void a(void);        

void f() { puts("main f"); }

static void sf() { puts("main sf"); }

void m() {
    f();
    sf();
}

int main() {
    m();
    a();
    return 0;
}

GitHub upstream.

Compile and run:

gcc -c a.c -o a.o
gcc -c main.c -o main.o
gcc -o main main.o a.o
./main

Output:

main f
main sf
main f
a sf

Interpretation

  • there are two separate functions sf, one for each file
  • there is a single shared function f

As usual, the smaller the scope, the better, so always declare functions static if you can.

In C programming, files are often used to represent "classes", and static functions represent "private" methods of the class.

A common C pattern is to pass a this struct around as the first "method" argument, which is basically what C++ does under the hood.

What standards say about it

C99 N1256 draft 6.7.1 "Storage-class specifiers" says that static is a "storage-class specifier".

6.2.2/3 "Linkages of identifiers" says static implies internal linkage:

If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.

and 6.2.2/2 says that internal linkage behaves like in our example:

In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function.

where "translation unit" is a source file after preprocessing.

How GCC implements it for ELF (Linux)?

With the STB_LOCAL binding.

If we compile:

int f() { return 0; }
static int sf() { return 0; }

and disassemble the symbol table with:

readelf -s main.o

the output contains:

Num:    Value          Size Type    Bind   Vis      Ndx Name
  5: 000000000000000b    11 FUNC    LOCAL  DEFAULT    1 sf
  9: 0000000000000000    11 FUNC    GLOBAL DEFAULT    1 f

so the binding is the only significant difference between them. Value is just their offset into the .bss section, so we expect it to differ.

STB_LOCAL is documented on the ELF spec at http://www.sco.com/developers/gabi/2003-12-17/ch4.symtab.html:

STB_LOCAL Local symbols are not visible outside the object file containing their definition. Local symbols of the same name may exist in multiple files without interfering with each other

which makes it a perfect choice to represent static.

Functions without static are STB_GLOBAL, and the spec says:

When the link editor combines several relocatable object files, it does not allow multiple definitions of STB_GLOBAL symbols with the same name.

which is coherent with the link errors on multiple non static definitions.

If we crank up the optimization with -O3, the sf symbol is removed entirely from the symbol table: it cannot be used from outside anyways. TODO why keep static functions on the symbol table at all when there is no optimization? Can they be used for anything?

See also

C++ anonymous namespaces

In C++, you might want to use anonymous namespaces instead of static, which achieves a similar effect, but further hides type definitions: Unnamed/anonymous namespaces vs. static functions

@M.M 2015-11-30 02:40:33

note: void f() { puts("sf"); } (i.e. two definitions of f()) causes undefined behaviour with no diagnostic required. It's a quality-of-linker issue to actually see an error message.

@Aqua 2019-12-11 04:42:38

This is the best and precise explanation! Than you!

@Dima 2009-02-17 18:44:01

There is a big difference between static functions in C and static member functions in C++. In C, a static function is not visible outside of its translation unit, which is the object file it is compiled into. In other words, making a function static limits its scope. You can think of a static function as being "private" to its *.c file (although that is not strictly correct).

In C++, "static" can also apply to member functions and data members of classes. A static data member is also called a "class variable", while a non-static data member is an "instance variable". This is Smalltalk terminology. This means that there is only one copy of a static data member shared by all objects of a class, while each object has its own copy of a non-static data member. So a static data member is essentially a global variable, that is a member of a class.

Non-static member functions can access all data members of the class: static and non-static. Static member functions can only operate on the static data members.

One way to think about this is that in C++ static data members and static member functions do not belong to any object, but to the entire class.

@Lightness Races in Orbit 2011-03-14 16:27:37

C++ has file-static, too. No need to bring C into this.

@Gerasimos R 2012-11-23 15:48:38

In C++, a static function is a static function. A static member function is a static member function, also known as a method. The fact that C does not have members does not mean that functions are "C".

@Alexander Malakhov 2013-03-15 10:52:07

is there any difference between global var and class static var (except namespace) ?

@Dima 2013-03-15 14:24:45

The namespace is the main difference. The other difference is that you can make a static data member private and thus only accessible from within the class' member functions. In other words, you have much more control over a static data member compared to a global variable.

@YoTengoUnLCD 2015-10-16 21:56:20

Could someone explain why thinking of a static function as private to its .c file not strictly correct? What's left to say?

@Dima 2015-10-16 21:57:49

@YoTengoUnLCD, what if your .c file #include's another .c file? By the way, this would make a good SO question in its own right. You should ask it.

@user2410022 2016-03-06 13:31:35

The answer to static function depends on the language:

1) In languages without OOPS like C, it means that the function is accessible only within the file where its defined.

2)In languages with OOPS like C++ , it means that the function can be called directly on the class without creating an instance of it.

@RobertS supports Monica Cellio 2020-06-08 12:12:27

This is not true. The explanation of your second paragraph refers to "static member functions" of a class, not "static functions". In C++, a function qualified with static has file-scope, too, like it is in C.

@Johannes Weiss 2009-02-17 18:27:31

static functions are functions that are only visible to other functions in the same file (more precisely the same translation unit).

EDIT: For those who thought, that the author of the questions meant a 'class method': As the question is tagged C he means a plain old C function. For (C++/Java/...) class methods, static means that this method can be called on the class itself, no instance of that class necessary.

@Slava V 2009-02-17 18:31:44

Actually I didn't tag it c++, some of admins probably did, but it was about C++, so what's the difference in C++?

@Chuck 2009-02-17 18:40:57

C++ methods are often referred to as "member functions", so I agree that C++ introduces a little bit of ambiguity. It's not your fault — the language just uses the keyword for two different things.

@Johannes Weiss 2009-02-18 16:13:38

Thats just fine, but don't you agree that it way clear Slava meant a normal function?

@Daniel Daranas 2009-02-19 09:32:55

@Johannes Yes, because otherwise the question body doesn't make any sense at all. However, the question title makes many people think about the most common use of "static" in functions in C++, which is in member functions (because member functions are far more common than nonmember ones).

@Johannes Weiss 2009-02-19 12:50:27

Daniel, yes. thats true. When I 'opened' the question I was thinking I'll explain class-static. Then I read the question and realized that I have to explain normal-function-static ;-). But remember: The question has be retagged by someone else to C++.

@Lightness Races in Orbit 2011-03-14 16:26:43

No, he still means a C++ function. A C++ free function rather than a C++ member function.

@ShreevatsaR 2015-01-27 18:49:50

@Chuck: C++ terminology never uses the word "method"; that's Java terminology -- in C++ standard documents it is always called "member function" (see this answer or this glossary of C++ vs Java terms (e.g. C++ uses "data member" and Java uses "field", etc)).

@M.M 2015-11-30 02:38:50

To clarify this answer a bit: the name of the function is only visible to other parts of the same translation unit, below the first declaration of that name. The function may be called from other units (and earlier parts of the same unit) via other means, e.g. a function pointer.

@TomSawyer 2020-04-26 20:25:09

why if i include that file, i can access to that function. so the static is useless now? calling function without include can cause warning: implicit declaration of function * is invalid in C99

@DylanYoung 2020-05-11 14:26:09

If you include the file, then it's declared in the current file and so you can access it. Read up on the preprocessor: includes are not imports.

@Douglas Leeder 2009-02-17 18:40:41

First: It's generally a bad idea to include a .cpp file in another file - it leads to problems like this :-) The normal way is to create separate compilation units, and add a header file for the included file.

Secondly:

C++ has some confusing terminology here - I didn't know about it until pointed out in comments.

a) static functions - inherited from C, and what you are talking about here. Outside any class. A static function means that it isn't visible outside the current compilation unit - so in your case a.obj has a copy and your other code has an independent copy. (Bloating the final executable with multiple copies of the code).

b) static member function - what Object Orientation terms a static method. Lives inside a class. You call this with the class rather than through an object instance.

These two different static function definitions are completely different. Be careful - here be dragons.

@Slava V 2009-02-17 18:44:23

Well, I do it just to clear up some space TEMPORARILY in main.cpp until I decide how to organize the file into libraries along with proper .hpp's. Is there a better idea how to do this?

@Brian Neal 2009-02-17 19:40:34

The correct terminology in C++ is member function, not method. There are no "methods" in C++ legalese. Method is a general OO term. C++ implements them via member functions.

@dersimn 2013-04-25 22:49:07

The following is about plain C functions - in a C++ class the modifier 'static' has another meaning.

If you have just one file, this modifier makes absolutely no difference. The difference comes in bigger projects with multiple files:

In C, every "module" (a combination of sample.c and sample.h) is compiled independently and afterwards every of those compiled object files (sample.o) are linked together to an executable file by the linker.

Let's say you have several files that you include in your main file and two of them have a function that is only used internally for convenience called add(int a, b) - the compiler would easily create object files for those two modules, but the linker will throw an error, because it finds two functions with the same name and it does not know which one it should use (even if there's nothing to link, because they aren't used somewhere else but in it's own file).

This is why you make this function, which is only used internal, a static function. In this case the compiler does not create the typical "you can link this thing"-flag for the linker, so that the linker does not see this function and will not generate an error.

@Brian Neal 2009-02-17 18:47:26

There are two uses for the keyword static when it comes to functions in C++.

The first is to mark the function as having internal linkage so it cannot be referenced in other translation units. This usage is deprecated in C++. Unnamed namespaces are preferred for this usage.

// inside some .cpp file:

static void foo();    // old "C" way of having internal linkage

// C++ way:
namespace
{
   void this_function_has_internal_linkage()
   {
      // ...
   }
}

The second usage is in the context of a class. If a class has a static member function, that means the function is a member of the class (and has the usual access to other members), but it doesn't need to be invoked through a particular object. In other words, inside that function, there is no "this" pointer.

@Deqing 2013-07-27 03:55:28

The question is about static in c.

@Brian Neal 2013-07-27 17:52:03

@Deqing the question was originally tagged C++ and the author talks about using ".cpp" files.

@dirkgently 2009-02-17 18:50:57

Minor nit: static functions are visible to a translation unit, which for most practical cases is the file the function is defined in. The error you are getting is commonly referred to as violation of the One Definition Rule.

The standard probably says something like:

"Every program shall contain exactly one definition of every noninline function or object that is used in that program; no diagnostic required."

That is the C way of looking at static functions. This is deprecated in C++ however.

In C++, additionally, you can declare member functions static. These are mostly metafunctions i.e. they do not describe/modify a particular object's behavior/state but act on the whole class itself. Also, this means that you do not need to create an object to call a static member function. Further, this also means, you only get access to static member variables from within such a function.

I'd add to Parrot's example the Singleton pattern which is based on this sort of a static member function to get/use a single object throughout the lifetime of a program.

@Parrots 2009-02-17 18:28:32

A static function is one that can be called on the class itself, as opposed to an instance of the class.

For example a non-static would be:

Person* tom = new Person();
tom->setName("Tom");

This method works on an instance of the class, not the class itself. However you can have a static method that can work without having an instance. This is sometimes used in the Factory pattern:

Person* tom = Person::createNewPerson();

@Slava V 2009-02-17 18:33:07

It seems to me that you are talking about static "method", not "function"??

@Parrots 2009-02-17 18:45:29

I assumed you were referring to static functions within a class.

@Slava V 2009-02-17 18:47:57

If I have known "methods" are called "method functions" in C++, I'd be more clear on that. Well, now I do :) Thanks anyway

@Brian Neal 2009-02-17 19:38:47

There are no "methods" in C++, just functions. The C++ standard doesn't ever mention "methods", just "functions".

@Puddle 2019-02-17 18:26:02

@BrianNeal a method is a class function. if it's not static it implicitly takes in the instance of the object (this), so that you can access the members. and the compiler implicitly refers to the members without using this-> unless you made a local variable in that method. i think you mean C since that doesn't even have classes.

@Brian Neal 2019-02-22 15:05:48

@Puddle I know what you are saying but in the C++ standard there is no definition of a "method". C++ only has functions, of various kinds. "Method" is a general OO term and is used in other languages and informally in C++. A method is formally known as a "member function" in C++.

@raimue 2009-02-17 18:28:44

static function definitions will mark this symbol as internal. So it will not be visible for linking from outside, but only to functions in the same compilation unit, usually the same file.

Related Questions

Sponsored Content

34 Answered Questions

[SOLVED] What's the difference between a method and a function?

24 Answered Questions

[SOLVED] What is the difference between call and apply?

37 Answered Questions

[SOLVED] What is dependency injection?

21 Answered Questions

[SOLVED] Are static class variables possible in Python?

25 Answered Questions

[SOLVED] What is the "-->" operator in C++?

19 Answered Questions

[SOLVED] What does "static" mean in C?

  • 2009-02-21 06:47:52
  • David
  • 921026 View
  • 1153 Score
  • 19 Answer
  • Tags:   c syntax static

39 Answered Questions

[SOLVED] var functionName = function() {} vs function functionName() {}

10 Answered Questions

[SOLVED] What is JSONP, and why was it created?

39 Answered Questions

[SOLVED] Difference between static class and singleton pattern?

21 Answered Questions

[SOLVED] What is reflection and why is it useful?

Sponsored Content