Explain static functions in C

The word static is used both in procedural and object oriented languages. In C it has a different meaning when used with a variable or when used with a function. Because this question is about static functions in C, so this is how we can explain this concept.

Below are some of the rules which applies to functions in C

  1. C functions are global by default. A function created in one file can be used in another file
  2. When we make a function static, it becomes a local function. This means that it cannot be used in any other file except the file in which it has been created.
  3. If code file File1.c and File2.c are using each others functions, then they can have two functions with the same name in them provided that both these functions are marked as static.

Look at this diagram

Image

 

In the diagram shown above, Fun( ) and Fun2( ) functions inside File1.c and File2.c respectively are global functions. They can be used in any file which contains code written in C language.

Fun1( ), Fun3( ) have been marked as static. So they can only be used in their own files and not outside their file.

Fun1( ) is present both in File1.c and File2.c. If File1.c and File2.c are using each other, then having a function with same name in both these files will not cause any problems because Fun1( ) has been marked static in both these files.

To conclude, you should only make a function static when you want its scope to be limited to its own file.

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp