What is a static function?

A static function is a function whose scope is limited to the current source file. Scope refers to the visibility of a function or variable. If the function or variable is visible outside of the current source file, it is said to have global, or external, scope. If the function or variable is not visible outside of the current source file, it is said to have local, or static, scope.

Showing Answers 1 - 22 of 22 Answers

prashant nagar

  • Jul 7th, 2006
 

also if a variable is declared to be static, then its value doesn't change within that function.

  Was this answer useful?  Yes

yba

  • Jul 9th, 2007
 

"also if a variable is declared to be static, then its value doesn't change within that function." is not correct.

The question wasn't about variables, however, a static variable declared within a function is certainly not constant because it is static.  A static declaration within a function directs the compiler to place the variable in persistant (global) storage, instead of placing it on the stack (automatic storage class).  The value will be retained from call to call, unlike an automatic variable, and only initialized once.

A static variable can also be defined/declared at file scope, where its name will be visible only within that file and where it has persistent storage (but will not be linked with extern variables of the same name).

It has been said that 'static' means too many things in C, but none of those meanings is 'const'.


  Was this answer useful?  Yes

yba

  • Jul 9th, 2007
 

The comment 'static variables will only be accessed by static functions' is not correct.

A non-static function may contain static variables, for example.  These variables have persistent storage class and their names are, as normal, visible inside the function.

A static variable defined at file scope, outside any function, can be accessed by any function that follows it in the file, whether that function is static or not.

  Was this answer useful?  Yes

abhishek sahu

  • Oct 23rd, 2007
 

According to the answer, static function's scope is limited to the current file. But this is the same case for normal function (which is not global).
Then what is the difference in static function and normal function?

  Was this answer useful?  Yes

i_spy

  • Dec 25th, 2007
 

Suppose void a(){} is defined in file1.c. If you declare it extern in file2.c then you can use it in file2.c. So its scope in not within file1.c. But if you declare it as static in file1.c then it can not be used in any other file.

  Was this answer useful?  Yes

With in the module it will comes upto end of the program.we can't access in another module.Number of functions it can access the static variable and sustaining that variable value.It can be declared as Local and Global.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions