Click here to Skip to main content
15,906,081 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Difference between static function and normal function?and give Example.
Posted

In C, a static function is only visible within the same file. As far as other files are concerned it does not exists at all.
You can't give an example for that because we don't have files here!

If you meant C#, then static has a different meaning.
For C#, static means that there is a single instance of the method, which is not related to any instance of the class. You access a ststic method via teh class name, instead of via a class instance:
C#
class MyClass
   {
   static void sm(){}
   void nm(){}
   ...
   }

MyClass instance = new MyClass();
instance.nm();  // Valid
MyClass.nm();   // Illegal - needs an instance
MyClass.sm();   // Valid


Think about cars: you can ask the question "how many wheels has a car?" because all cars have four wheels. "HowManyWheels" is a static method of the Car class.
But you can't ask "what colour is a car?" because you need to specify which car you are talking about: "what colour is my car?" or "what colour is that car?"
 
Share this answer
 
Comments
nv3 16-Jun-12 10:57am    
Your beat me for just 7 min :-) 5.
bchandu06 18-Jun-12 13:53pm    
thanks..
In the context of C (and not C++) a static function is only known within its own source file. We say, its "scope" is limited to the source file.

A "normal" function, in contrast, can be accessed from code in other source file, given that there is a declaration in a header file (or other place) that makes them known within that source file. The linker is responsible for inserting the correct addresses of such functions.
 
Share this answer
 
Comments
nv3 17-Jun-12 4:50am    
Would the user who has downvoted my answer with a 1 please be so kind to leave a comment, why he or she thinks that my answer is so unsufficient. I would be interested in which regard my answer is incorrect and others would probably also benefit from that knowledge.
bchandu06 18-Jun-12 13:52pm    
thanks
nv3 18-Jun-12 14:02pm    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900