Detailed explanation of static keyword in C language

Time:2022-7-22

The staic keyword in C language is very simple, so simple that you can not write a staic keyword in any project. Writing this chapter is mainly about my understanding and application of staic. Of course, at the beginning of the chapter, I still need to briefly describe the static keyword according to the book.

1. Brief description

Static keyword is very simple in embedded C language. It has three functions:

Function 1: in the function body, a variable declared as static maintains its value during the call of this function.

void test1(void)
{
    unsigned char a;
    static unsigned char b;
    …
    a++;
    b++;
}

In this example, variable a is a local variable and variable B is a local static variable. Function 1 describes the characteristics of local static variable B: in the function body, a variable declared as static (that is, a local static variable) maintains its value during the call of this function. At this time, the effect is the same as that of the global variable.

In the following simple example, declare a local variable of the function and set it as static type as a counter, so that the function can be counted every time it is called. This statistical method is very good, because other functions can modify this variable (except for the program running and flying).

void count();
int main()
{
    int i;
    for (i = 1; i <= 3; i++)
    {
        count();
    {
     return 0;
}
void count()
{
    static num = 0;
    num++;
    printf(" I have been called %d",num,"times/n");
}

The output result is:

I have been called 1 times.
I have been called 2 times.
I have been called 3 times.

Here are two points to note:

The effect of local static quantity is still changed and still valid within the number

The storage location of local static variables becomes memory and is no longer stored in the stack

On the storage of local static variables, students who don’t understand it move to the previous chapter《Memory allocation of C language in stm32》。

Function 2: in the module (but outside the function), a variable declared as static can be accessed by the function used in the module, but cannot be accessed by other functions outside the module. It is a local global variable.

Variables defined in this way are also called global static variables: add the keyword static before the global variable, and the global variable is defined as a global static variable. That is, the static variables declared in the module (but outside the function) mentioned in function 2 above.

Benefits of defining global static variables:

Its scope is only in this file and will not be accessed or modified by other files. It is a local variable.

Variables with the same name can be used in other files without conflict.

Function 3: in a module, a function declared as static can only be called by other functions in this module. That is, this function is restricted to the local scope of the module that declares it.

The function defined in this way also becomes a static function: add the keyword static before the return type of the function, and the function is defined as a static function.

Benefits of defining static functions:

Functions with the same name can be defined in other files without conflict

Static functions cannot be used by other files. It defines a local function.

2. Talk more

As mentioned above, the static keyword is very simple, which is basically for code modularization. For quite a period of time, I didn’t use the static keyword in my work. In the code of MCU, generally one person can manage the whole code project, so I didn’t use the static keyword a lot.

In previous articles《STM32 “hidden timer” -dwt》The code for measuring the running time of the code is as follows:

float DTW_Time_Difference_ms(void)
{
  static uint32_t old_counter;
  uint32_t counter,couter_current;
  couter_current = DWT_TS_GET();
  if(couter_current > old_counter)
    counter = couter_current - old_counter;
  else
    counter = couter_current + 0XFFFFFFFF - old_counter;
  old_counter = couter_current;
  return (counter / (SystemCoreClock/1000));
}

Using the static keyword, I also realized the benefits of using the static keyword, which is good for function encapsulation. When you need to encapsulate a function function to the extent that you can use it by directly copying it, you need a full variable at this time, and a static local variable in the function at this time.

Everyone who reads the article, if you have the wonderful use of static keyword, please leave a message in the comment area!

Click to view the album of this article:Advanced C language