The “static” keyword before a function name makes it static.For example, below function fun() is static. When func_1() is called for the first time b is initialized to 100, in line 22, the value of b is incremented.
The default value of static variables is zero.
Don’t stop learning now. Then the value of num is displayed and num is incremented by one.
Static variables can be defined inside or outside the function. acknowledge that you have read and understood our In func_1(), the variable b is declared as a static. By using our site, you
In the function func(), num is a static variable that is initialized only once. This program works correctly as the static function is called only from its own object file. A static function in C is a function that has a scope that is limited to its object file. Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
This new value of b will be retained the next time the func_1() is called. We use cookies to ensure you have the best browsing experience on our website. So if you save the program as a C++ program, it would compile \and run fine.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.Attention reader! The main() function calls staticFunc(). In this article, we will discuss the ‘static’ storage class and explain how to use static variables and static functions in C with some sample code snippets. Here is the syntax of static variables in C language, static datatype variable_name = value; Here,
Get hold of all the important DSA concepts with the
A function can be declared as static function by placing the static keyword before the function name.An example that demonstrates this is given as follows −There are two files first_file.c and second_file.c. This means that the static function is only visible in its object file. Prerequisite : Static variables in C In C, functions are global by default. A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.
An example that demonstrates this is given as follows − This happens as the function staticFunc() is a static function and it is only visible in its object file.A program that demonstrates static functions in C is given as follows −In the above program, the function staticFunc() is a static function that prints ”Inside the static function staticFunc()”. Following are some interesting facts about static variables in C. 1) A static int variable remains in memory while the program is running. Before moving ahead, lets quickly understand the difference between life time and scope of a variable. Static variables in a Function: When a variable is declared as static, space for it gets allocated for the lifetime of the program.Even if the function is called multiple times, space for the static variable is allocated only once and the value of variable in the previous call gets carried through the next function … Static variables have a property of preserving their value even after they are out of their scope!For example, we can use static int to count a number of times a function is called, but an auto variable can’t be used for this purpose.Please note that this condition doesn’t hold in C++.
The static variables are alive till the execution of the program.
The contents of these files are given as follows −Now, if the above code is compiled then an error is obtained i.e “undefined reference to staticFunc()”. A normal or auto variable is destroyed when a function call where the variable was declared is over. They are local to the block. The code snippet for this is given as follows − void func() { static int num = 1; cout <<"Value of num: "<< num <<"\n"; num++; } In the function main(), the function func() is called 3 times.