Prerequisite : Static variables in C
In C, functions are global by default. The “static” keyword before a function name makes it static. For example, below function fun() is static.
Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.
For example, if we store following program in one file file1.c
And store following program in another file file2.c
Now, if we compile the above code with command “gcc file2.c file1.c”, we get the error “undefined reference to `fun1’” . This is because fun1() is declared static in file1.c and cannot be used in file2.c.
Please write comments if you find anything incorrect in the above article, or want to share more information about static functions in C.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Can static functions be virtual in C++?
- Internal static variable vs. External static variable with Examples in C
- Static Variables in C
- C++ | Static Keyword | Question 6
- C++ | Static Keyword | Question 1
- C++ | Static Keyword | Question 2
- C++ | Static Keyword | Question 3
- C++ | Static Keyword | Question 4
- C++ | Static Keyword | Question 5
- Static data members in C++
- Initialization of static variables in C
- When are static objects destroyed?
- “static const” vs “#define” vs “enum”
- Difference between Static and Shared libraries
- What are the default values of static variables in C?
- How are variables scoped in C - Static or Dynamic?
- Initialization of global and static variables in C
- Count the number of objects using Static member function
- Functions in C/C++
- Thread functions in C/C++