top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is a static function in c ?

0 votes
683 views
What is a static function in c ?
posted Jan 16, 2015 by Chirag Jain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes

Static function limits the scope of the function to the file.

answer Jan 16, 2015 by Pdk
+1 vote

Just adding on what PDK has explained?

Every function is global by default until unless we specify the keyword “static” keyword before function name to make it static.

Example

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

Static functions are restricted to the file where they are declared hence can be accessed by the other function of the same file but not by the other files.

Example

/* Inside file1.c */
static void StaticFunc(void)
{
  puts("StaticFunc called");
}

/* Iinside file2.c  */
int main(void)
{
  StaticFunc(); // Error
  return 0;  
}
answer Jan 17, 2015 by Salil Agrawal
Well, that explains it.
Thank you.
Similar Questions
+3 votes

Please provide some details on the static class variable, how it works and any example would be great.

+5 votes

You can find three principal uses for the static. This is a good way how to start your answer to this question. Let’s look at the three principal uses:

  1. Firstly, when you declare inside of a function: This retains the value between function calls

  2. Secondly, when it is declared for the function name: By default function is extern..therefore it will be visible out of different files if the function declaration is set as static..it will be invisible for outer files

  3. And lastly, static for global parameters: By default you can use global variables from outside files When it’s static global..this variable will be limited to within the file.

is this right ?

...