what exactly this means....i mean to say if we declare a function as static what are the spl properties that are aquired by the funcion....

June 3rd, 2003, 09:43 PM

slarty

A function marked as "static" is not available to the linker when linking a program with more than one module. This is useful because:

1. A static function can be declared as static in more than one module, and do different things without causing a clash
2. A static function declared in a library is not exported to the program using it, so it won't clash with another function of the same name, and the program cannot call it (unless they know the address which is highly unlikely)
3. Your program may build slightly faster and / or smaller because the compiler will decide how to optimise it at compile time rather than leaving it for the linker

In main.c you cannot call wibble() because it is declared as static in a different module.

If you had a function wobble() declared in main.c and util.c, the linker would give an error. If you declared them both static, it would be fine.

Edit:

It is good practice to declare *everything* static unless you're using it in another module of your multi-module program.

That way, if there are other developers working on some other code, you're less likelly to have name clashes.

Edit 2:

You must not declare any functions which are going to be called by name from outside your program (notably main(), WinMain() and similar) as static, or the program won't link

June 3rd, 2003, 09:51 PM

ammo

Static takes many meanings in C and even more in C++...

Specifically, in C, declaring a *global* vraiable or a function as static means that it exists only in that module. In other words, that variable/function, declared in mod1.c (for example), cannot be accessed/called from mod2.c. It will also not conflict with vraiables/functions of the same name declared in other modules.

In the case of a local variable, it means that the variable is "persistent" throughout all calls made to the function in which it is declared (typical example would be to count the number of calls made to that function...)

In C++ and other OO languages, class variables and methodes declared as static means that they can be accessed without having to instanciate an instance of that class; you can call/access them by ClassName.var or ClassName.methode()...