C language static - Introduction to C language zero Foundation

Keywords: C static

catalogue

Zero foundation C/C + + learning route recommendation: C/C + + Learning directory >> Introduction to basic C language

1, Introduction to static

stay C language In, static Keywords can be used not only to modify variables, but also to modify functions. When using the static keyword to modify a variable, we call it a static variable.

1. If a local variable is modified with static, the variable is a local static variable;

#include <stdio.h>

int main()
{
   static int x = 0; //Local static variable
   printf("www.codersrc.com");
   return 0;
}

2. If the global variable is decorated with static, the variable is a global static variable;

#include <stdio.h>

static int x = 0; //Global static variable

int main()
{
   printf("www.codersrc.com");
   return 0;
}

3. If the function is decorated with static, the function is a static function;

/******************************************************************************************/
//@Author: ape programming
//@Blog (personal blog address): www.codersrc.com
//@File:C language tutorial - C language static
//@Time:2021/07/18 07:30
//@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly!
/******************************************************************************************/


#include <stdio.h>


static int func() //Static function
{
    printf("Static function");
    return 0;
}

int main()
{
   func();
   printf("www.codersrc.com");
   return 0;
}
/*
Output: static function www.codersrc.com
*/

2, static modifier variable

1.static local static variable

Variables defined inside a function are called local variable (Local Variable), whose scope is limited to the inside of the function, leaving the function function After that, it is invalid. If the life cycle ends directly, an error will be reported if it is used again.

The scope of a local static variable modified with static is limited to the inside of the function function Then it is invalid, * * life cycle * * until the end of the program, for example:

/******************************************************************************************/
//@Author: ape programming
//@Blog (personal blog address): www.codersrc.com
//@File:C language tutorial - C language static
//@Time:2021/07/18 07:30
//@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly!
/******************************************************************************************/


#include <stdio.h>


int func1()
{
    int x = 5;
    x++;
    printf("function func1 local variable x = %d\n",x);
    return 0;
}

int func2()
{
    static int x = 10;
    x++;
    printf("function func2 Static local variable x = %d\n",x);
    return 0;
}

int main()
{

    for(int i = 0;i<10;i++)
    {
       func1();
       func2();
    }
    return 0;
}
/*
Output:

Function func1 local variable x = 6
 Function func2 static local variable x = 11
 Function func1 local variable x = 6
 Function func2 static local variable x = 12
 Function func1 local variable x = 6
 Function func2 static local variable x = 13
 Function func1 local variable x = 6
 Function func2 static local variable x = 14
 Function func1 local variable x = 6
 Function func2 static local variable x = 15
 Function func1 local variable x = 6
 Function func2 static local variable x = 16
 Function func1 local variable x = 6
 Function func2 static local variable x = 17
 Function func1 local variable x = 6
 Function func2 static local variable x = 18
 Function func1 local variable x = 6
 Function func2 static local variable x = 19
 Function func1 local variable x = 6
 Function func2 static local variable x = 20
*/

There is a variable x in both func 1 and func 2. According to the output results, whether it is an ordinary local variable or a static local variable modified by static, the scope is only valid inside the function;

It can be seen that the effect of static local variables is the same as that of global variables, but the scope is only limited to the function body;

2.static global static variable

  • 1. The static global variable is initialized only once;
  • 2. In a single. h or. c file, common global variables are the same as static modified global static variables;
  • 3. When a program is composed of multiple. h or. c files, the global variables are valid in each source file, while the global static variables modified by static can only be used in the current. h or. c file, and the global variables with the same name declared or defined in other files will not conflict, so as to reduce the program coupling;
  • 4.static Modified variables are stored in the static data area. The default value of all bytes in memory is 0x00 (the same is true for global variables). Therefore, the default value of global variables and static modified variables is 0.
/********************************************\*\*********************************************/
  //@Author: ape programming
  //@Blog (personal blog address): www.codersrc.com
  //@File:C language tutorial - C language static
  //@Time:2021/07/18 07:30
  //@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly!
  /********************************************\*\*********************************************/

  #include <stdio.h>

  static int x; //static global
  int y; //global variable
  int main(void)
  {
  static int z; //Static local variable
  printf(" x: %d\n y: %d\n z: %d",x,y,z);
  return 0;
  }

  /*

  Output:
  x: 0
  y: 0
  z: 0
  */

3, static modifier function

  • A static function can only be visible in the file that declares it. Other files cannot reference it function
  • Different files can use static functions with the same name without affecting each other

The following two file examples illustrate the use of static A declared function cannot be referenced by another file:

/******************************************************************************************/
//@Author: ape programming
//@Blog (personal blog address): www.codersrc.com
//@File:C language tutorial - C language static
//@Time:2021/07/18 07:30
//@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly!
/******************************************************************************************/


/* file1.c */
#include <stdio.h>

static void fun(void)
{
    printf("hello from fun.\n");
}

int main(void)
{
    fun();
    fun1();

    return 0;
}

/* file2.c */
#include <stdio.h>

static void fun1(void)
{
    printf("hello from static fun1.\n");
}
/*
Output:
error: file1.c:(.text+0x20): Undefined reference to 'fun1'
collect2: error: ld returned 1 exit status
*/

Modify the file without using the static modifier. You can refer to this function in another file:

/******************************************************************************************/
//@Author: ape programming
//@Blog (personal blog address): www.codersrc.com
//@File:C language tutorial - C language static
//@Time:2021/07/18 07:30
//@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly!
/******************************************************************************************/


/* file1.c */
#include <stdio.h>

static void fun(void)
{
    printf("hello from fun.\n");
}

int main(void)
{
    fun();
    fun1();

    return 0;
}

/* file2.c */
#include <stdio.h>

void fun1(void)
{
    printf("hello from static fun1.\n");
}

/*
Output:
hello from fun.
hello from static fun1.
*/

4, Guess you like it

  1. Difference between C language array subscript out of bounds and memory overflow
  2. C language uses pointers to traverse arrays
  3. Difference between C language pointer and array
  4. Difference between C language pointer array and array pointer
  5. C language field pointer
  6. C language function value transfer and address transfer
  7. C language function indefinite length parameter
  8. C language function pointer
  9. C language pointer function
  10. C language callback function callback
  11. C language #pragma once
  12. Difference between C language #include < > and #include ""
  13. C language const modifier function parameters
  14. Difference between const and define in C language
  15. C language # operators
  16. C language ## operators
  17. C language__ VA_ARGS__
  18. C language##__ VA_ARGS__
  19. C language function indefinite length parameter##__ VA_ARGS__ Classic case
  20. C language va_start / va_end / va_arg custom printf function
  21. C language main function
  22. C language main function parameter main(int argc, char *argv [])
  23. C language local variables
  24. C language global variables
  25. Differences between global variables and local variables in C language
  26. C language static

No reprint without permission: Ape programming ยป C language static

Posted by exnet on Fri, 24 Sep 2021 18:06:00 -0700