Open In App

Static functions in C

Last Updated : 23 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In C, functions are global by default. The “static” keyword before a function name makes it static.

For example, the below function fun() is static.

C




static int fun(void) {
  printf("I am a static function ");
}


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 the reuse of the same function name in other files.

For example, if we store the following program in one file file1.c

C




/* Inside file1.c */
static void fun1(void) {
  puts("fun1 called");
}


And store the following program in another file file2.c

C




/* Inside file2.c  */
int main(void)
{
  fun1();
  getchar();
  return 0; 
}


Now, if we compile the above code with the 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.

Related Articles:



Previous Article
Next Article

Similar Reads

Internal static variable vs. External static variable with Examples in C
The static variable may be internal or external depending on the place of declaration. Both kinds of static variables are stored in initialized data segments. Internal Static Variables Internal Static variables are defined as those having static variables which are declared inside a function and extend up to the end of the particular function. Synt
2 min read
Can Static Functions Be Virtual in C++?
In C++, a static member function of a class cannot be virtual. Virtual functions are invoked when you have a pointer or reference to an instance of a class. Static functions aren't tied to the instance of a class but they are tied to the class. C++ doesn't have pointers-to-class, so there is no scenario in which you could invoke a static function v
1 min read
How are variables scoped in C - Static or Dynamic?
In C, variables are always statically (or lexically) scoped i.e., binding of a variable can be determined by program text and is independent of the run-time function call stack. For example, output for the below program is 0, i.e., the value returned by f() is not dependent on who is calling it. f() always returns the value of the global variable x
1 min read
What are the default values of static variables in C?
In C, if an object that has static storage duration is not initialized explicitly, then: — if it has pointer type, it is initialized to a NULL pointer; — if it has an arithmetic type, it is initialized to (positive or unsigned) zero; — if it is an aggregate, every member is initialized (recursively) according to these rules; — if it is a union, the
2 min read
Static Variables in C
Static variables have the property of preserving their value even after they are out of their scope! Hence, a static variable preserves its previous value in its previous scope and is not initialized again in the new scope. Syntax: static data_type var_name = var_value;Following are some interesting facts about static variables in C: 1) A static in
4 min read
C++ | Static Keyword | Question 1
Predict the output of following C++ program. #include <iostream> using namespace std; class Test { static int x; public: Test() { x++; } static int getX() {return x;} }; int Test::x = 0; int main() { cout << Test::getX() << " "; Test t[5]; cout << Test::getX(); } (A) 0 0 (B) 5 5 (C) 0 5 (D) Compiler Error Answer: (
1 min read
C++ | Static Keyword | Question 2
#include <iostream> using namespace std; class Player { private: int id; static int next_id; public: int getID() { return id; } Player() { id = next_id++; } }; int Player::next_id = 1; int main() { Player p1; Player p2; Player p3; cout << p1.getID() << " "; cout << p2.getID() << " "; cout << p
1 min read
C++ | Static Keyword | Question 4
Predict the output of following C++ program. #include <iostream> using namespace std; class A { private: int x; public: A(int _x) { x = _x; } int get() { return x; } }; class B { static A a; public: static int get() { return a.get(); } }; int main(void) { B b; cout << b.get(); return 0; } (A) 0 (B) Linker Error: Undefined reference B::a
1 min read
C++ | Static Keyword | Question 5
#include<iostream> using namespace std; class Test { private: static int count; public: Test& fun(); }; int Test::count = 0; Test& Test::fun() { Test::count++; cout << Test::count << " "; return *this; } int main() { Test t; t.fun().fun().fun().fun(); return 0; } (A) Compiler Error (B) 4 4 4 4 (C) 1 1 1 1 (D) 1 2
1 min read
C++ | Static Keyword | Question 6
Output of following C++ program? #include <iostream> class Test { public: void fun(); }; static void Test::fun() { std::cout<<"fun() is static\n"; } int main() { Test::fun(); return 0; } Contributed by Pravasi Meet (A) fun() is static (B) Empty Screen (C) Compiler Error Answer: (C) Explanation: The above program fails in compi
1 min read
Article Tags :