Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a "static" function in C?

The question was about plain c functions, not c++ 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 "print_matrix@@....) 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.

like image 752
Slava V Avatar asked Feb 17 '09 18:02

Slava V


People also ask

What does a static function mean?

A static method (or static function) is a method defined as a member of an object but is accessible directly from an API object's constructor, rather than from an object instance created via the constructor.

Why do we need static functions in C?

Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.

What is static and dynamic function in C?

Static Memory Allocation is done before program execution. Dynamic Memory Allocation is done during program execution.

What is static function variable in C?

Static variables are the variables which once declared, they get destroyed only when the program has completed its execution. They have a property of retaining their previous scope value if they are already declared once in the program.


2 Answers

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.

like image 184
Johannes Weiss Avatar answered Sep 30 '22 22:09

Johannes Weiss


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.

like image 39
Dima Avatar answered Sep 30 '22 21:09

Dima