Wednesday, October 13, 2010

Static Function in C Language

,

By default any function that is defined in a C file is extern, which means they're visible across translation units. But when we use “static” keyword it restricts visibility of the function to the translation unit in which it's defined.

static int sfun(void)
{
  printf("I am a static function ");
}

when we want to restrict access to functions, we make them static. Static functions are functions that are only visible to functions in the same file and they are not callable from outside.

/* a.c */
static void sfun1(void)
{
  puts("sfun1 called");
}

And store following program in another file b.c

/* b.c  */
int main(void)
{
  sfun1();
  getchar();
  return 0;
}

we get the error “undefined reference to `sfun1′”. 

0 comments to “Static Function in C Language”

Post a Comment