Use cases of ‘static’ with functions
In this article, let’s see how static keywords can be used with functions. So, we have two files. A main.c and file1.c.
And I create one function in main.c. The function name void change_system_clock. This function takes the argument system_clock from other functions. So, don’t worry, you’ll learn more about functions later.
This function actually takes the system clock value from other functions in this variable(system_clock), and then it uses this variable to change the system clock.
Then, give the printf statement to print the system_clock here.
static int mainPrivateData; int main() { mainPrivateData = 100; printf("mainPrivateData = %d\n", maonPrivateData); file1_myFun1(); printf("mainPrivateData = %d\n", mainPrivateData); return 0; } void change_system_clock(int system_clock) { printf("system clock changed to = %d\n", system_clock); }
main.c file
For main.c, this is a very critical and important function, and this function should not be called from other files. The developer who wrote this function wants only functions of main.c should call this function. So, functions outside the file main.c should not call this function.
But, if you leave this function like this, any function of the project can call this function, and it can change the system clock and thus causing a problem to the application.
For example, in this case, we have file1.c, and the function of the file1.c can call the change_system_clock function with no issues. It can be called with 0.
So, all you need to do is just give the prototype of the void change_system_clock(int system_clock) function. Write over here and just call that function. You can even use the extern keyword here because you are calling a function which is implemented outside the scope of this file. So, you can even use the extern with this function prototype. But, an extern is usually assumed here. So, you need not use the keyword extern actually. So, it is assumed. Now, this code works perfectly fine.
Let’s run the code. You can see Figure 1. There are no errors. In file1.c, simply called the change_system_clock function, and it changed the system clock, which main.c doesn’t want to happen.
So, if main.c wants to protect calling this function outside the scope of this file, then you have to use the static keyword here. This is the static function, or it’s also called a private function of the file main.c. So, only functions of the main.c can call this function now. No function outside the scope of this file can call this function.
Let’s compile this program and see what happens. The compilation is not successful. The compiler is saying that undefined reference to ‘change_system_clock.’
So, the compiler is not letting file1.c to compile successfully. That’s an alert for the developer of file1.c to understand that he should not call the change_system_clock function because it’s a private function. That’s how privacy is imposed in ‘C.’ So, that’s about the usage of static.
In the following article, let’s explore another storage class specifier that is extern.
Get Full Course on Microcontroller Embedded C Programming Here.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1