GeekInterview.com
Series: Subject: Topic:
Question: 513 of 589

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.
Asked by: Interview Candidate | Asked on: Mar 6th, 2005
Showing Answers 1 - 8 of 8 Answers
prashant nagar

Answered On : Jul 7th, 2006

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

  
Login to rate this answer.
dasam

Answered On : Mar 30th, 2007

View all answers by dasam

static variables will only be accessed by static functions.

  
Login to rate this answer.
yba

Answered On : 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'.


  
Login to rate this answer.
yba

Answered On : 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.

  
Login to rate this answer.

Which static variables that are only accesed by static functions you are talking abt?

  
Login to rate this answer.
abhishek sahu

Answered On : 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?

  
Login to rate this answer.
i_spy

Answered On : Dec 25th, 2007

View all answers by i_spy

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.

  
Login to rate this answer.
srini.embsys

Answered On : Dec 25th, 2007

View all answers by srini.embsys

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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.