Static functions in C

Asked

Viewed 1,182 times

5

I noticed that some libraries use functions and static variables, for example static int sum(lua_State *state).

This function belongs to a library that will be compiled into the language Moon.

But also libraries with calls for dlsym also have static functions.

What the use of static functions in libraries provides different with non-static functions?

  • C has methods? I believe there must already be a question about that. If we are talking about OO, what is declared with the static modifier executes in the class context, and not the object.

  • It was a small syntax error, I’ve made the corrections

1 answer

7


C has no methods, only functions (the original question spoke in methods). Although C++ has methods, only functions are static. What is known by the static method in Java and C#, for example, is just a function in these languages.

In C the static in function is actually a shape similar to private. Since there are no classes, this applies to the file in which it was declared, so it can only be called there in that file.

Static variables mean they have global state. When a variable is declared with static it is neither placed in the stack, nor in the heap, yes in a static area, so it is already reserved by the compiler in the executable. The lifespan, of course, it is for the entire duration of the application. This is considered global state and may cause problems if the application has concurrent access, or even if it does not take certain care.

There are two possible visibilities when the variable is static.

If it is inside the function it will only be visible there. Every time you call the function this variable will have the last value it had on the last call, it is not destroyed at the end of its execution.

If outside the function will only be visible in the file, same as with the function.

In C it is rare to use static variables. Functions are very useful to decrease visibility. In C++ it is recommended to avoid both since it has better mechanisms.

There is no special use for libraries. It’s actually the opposite, as it makes it private, whoever consumes the library will never access it under normal conditions. The most that can be said is that it prevents access, but this is not unique to libraries.

Example:

#include <stdio.h>

static int externa = 1;

static void teste() {
    int x = 0;
    static int estatica = 0;
    x++;
    estatica++;
    printf("x = %d, estatica = %d, externa = %d\n", x, estatica, externa);
}
int main() {
    printf("externa = %d\n", externa);
    for (int i = 0; i < 10; i++) teste();
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Try creating another file, and calling the function teste() or the variable externa. It will not compile. Obviously it would need a header. If you include this file, then it will be part of the other file and compile.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.