• Pouya Neshagar

Use of ‘Static’ keyword in Embedded C

Updated: Apr 8

In this article, we will take a close look at the ‘Static’ keyword in C language and how it will affect our code in deeper level in Embedded C. We will get to know three different use cases and how applying the 'static' keyword will influence them.


Attention for beginners: -----------------------------------------------

Please bear in mind that you should know what are “Global Variables” and “Local Variables” and what it means when we talk about the “scope of a variable” or “scope of a function” in C language. If you need to refresh your memory, please kindly do so before going further.

----------------------------------------------------------------------------


Static Functions:

Imagine we have following function in our “learningStatic” module (learningStatic.c source file):


/* -- learningStatic.c -- */

#include something.h

#include somethingElse.h

void myFunction(void);


By default in C language, every function, would be visible to the whole code in the application. Meaning other functions in other source files are able to call the function and use it. So, in order to have a bit of control and be able to hide the function within the module (.c source file), we use the Static keyword.


Use of 'static' keyword makes the function local to the source file and only functions inside the module are able to access it. To make a function, a static function, we need to just add the ‘static’ keyword to the beginning of function definition.


/* -- learningStatic.c -- */

#include something.h

#include somethingElse.h

static void myLocalFunction(void);



Static Variables:

Look at the following code:


/* -- learningStatic.c -- */

#include something.h


int myVar;

void func(void)

{

int var_insideTheFunction;

}


‘myVar’ is an integer variable with the global scope and it is accessible to every function in the application. It stored in the main memory (static memory) on a fixed memory location.


On the other hand, the ‘var_insideTheFunction’, is an integer variable with the scope inside the function, meaning only members inside the function have access to it. It will exist as long as the function is alive, and will be stored in a temporary memory location, or stack memory.


Now let’s make them static and see how each will change:


/* -- learningStatic.c -- */

#include something.h


static int myVar;

void func(void)

{

static int var_insideTheFunction;

}


By applying the static keyword to ‘myVar, we have changed the variable’s scope. Now it’s a local variable and only accessible within the source file (module) and only functions and members inside the module can access it. It is still stored on a fixed memory location.


By applying the static keyword to ‘var_insideTheFunction’, we will change the memory location of the variable and now it will be stored on a fix memory location in main memory. It means that, although the scope is unchanged and still be accessible only within the function, BUT its life time will change, and it will survive even after existing from the function and between function calls.


Observation: -----------------------------------------------------

Try to create a function like the following. Call the function in 'main' several times and observe the output:


void func(void)

{

static int x=0;

int y=0;

x += 5;

y += 5;

printf("x = %d, y =%d \n", x ,y);

}


Can you explain the observations, by what you have learned here?

hmm... What happen to the 'x' initialization then? and why?

---------------------------------------------------------------------


To recap what we have learned:

As for functions, 'static' keyword, will change the scope of the functions to a local function. Meaning it will be only accessible with the functions inside the module ( .c file).


As for a global variable, 'static' keyword, will changed its scope to local variable and like the local function, it will be accessible only within the source file.


As for a local variable inside a function, 'static' keyword, will changed its memory location and make it to be store in a static memory (a fixed memory location). Its value will survive between function call.




Hope you find this article helpful. As always, I encourage you to ask WHY and look for background behaviors when you are trying to learn new concepts in Embedded C. And remember asking better questions, will help you to find better answers.

-------------------------------------------------------------------------------------

If you find this article useful, share it so that your friends can also benefit. It would be pleasure to have you as a subscriber, so make sure to subscribe to the newsletter to stay on top my featured blog posts each month.

© 2020   |   Helsinki, Finland   |  Pouya (at) PouyaNeshagar.com   |        | Legal: Cookies

  • LinkedIn Social Icon