In article <11*********************@g47g2000cwa.googlegroups. com>, "John Bode" <jo*******@my-deja.com> writes:
prasi wrote: I wanted to know the significance of static functions.
In C, a static function cannot be referenced outside of its own
translation unit; the function name is not exported to the linker.
Cannot be referenced *by name*. "static" applies to the name of the
function; it doesn't do anything to the function itself. That should
be obvious to any experienced C practitioner, but might not be to
beginners.
So, for example, a static function in one translation unit can be
called from another translation unit through a function pointer,
which might have been returned by a (non-static) function in the same
TU as the static function, or might even be a global variable in
the first TU:
--- inc.c ---
static int inc(int x) { return x+1; }
int (*incfunc)(int) = inc;
---
--- main.c ---
#include <stdio.h>
extern int (*incfunc)(int);
int main(void)
{
printf("%d\n", incfunc(1));
return 0;
}
---
This is actually useful for some abstraction purposes. For example,
in a real inc.c, there might be multiple implementations of the "inc"
function, all of which are hidden (via static) from other TUs. At
program startup, incfunc(x) invokes the default implementation, but a
(non-static) function in inc.c might be available to select a
different one for subsequent calls. Other TUs don't need to know
anything about this mechanism, only the API for using it.
--
Michael Wojcik
mi************@microfocus.com
Even 300 years later, you should plan it in detail, when it comes to your
summer vacation. -- Pizzicato Five