C storage classes and scopes

1. Scope

In any kind of programming, the scope is the area where the variables defined in the program exist. Variables cannot be accessed beyond this area. There are three places in C language where variables can be declared:
Local variables< inside functions or blocks a i=4> Global variables outside all functions In formal parameters Function parametersdefining

local variables

Variables declared inside a function or block are called local variables. They can only be used by statements inside the function or block of code.
Local variables are not known outside the function.

global variables

Global variables are defined outside the function, usually at the top of the program. Global variables are valid throughout the program life cycle and can be accessed within any function.
In a program, the names of local variables and global variables can be the same, but within a function, if the two names are the same, the local variable value will be used, and the global variable will not be used.

#include <stdio.h>
 
/* 全局变量声明 */
int g = 20;
 
int main ()
{
  /* 局部变量声明 */
  int g = 10;
 
  printf ("value of g = %d\n",  g);
 
  return 0;
}
输出:
value of g = 10

Formal parameters

The parameters of a function, formal parameters, are treated as local variables within the function. If they have the same name as global variables, they will be used first.

2.Storage category

The storage classes available in C programs are listed below:
auto
register
static
extern

auto storage class

auto is the default storage class for all local variables
Variables defined in a function default to the auto storage class, which means they are created at the beginning of the function and at the end of the function be destroyed.

{
	int mount;
    auto int month;
}
# auto 只能用在函数内,即 auto 只能修饰局部变量

register storage class

register storage class definition is stored inregister, so the access speed of variables is faster, but it cannot directly take the address because it is not a storage in RAM. Using the register storage class on variables that need to be accessed frequently can improve the running speed of the program.
This means that the maximum size of the variable is equal to the size of the register (usually a word), and the unary '&' operator cannot be applied to it (because it has no memory location).

register int  miles;

Registers are only used for variables that need to be accessed quickly, such as counters. It should also be noted that defining 'register' does not mean that the variable will be stored in a register, it means that the variable may be stored in a register, depending on hardware and implementation limitations.

static storage class

The static storage class instructs the compiler to keep a local variable alive for the lifetime of the program without having to create and destroy it every time it enters and leaves scope.
static Modify local variables to maintain the value of local variables between function calls.
The static modifier can also be applied to global variables. When static is used to modify a global variable, the scope of the variable is limited to the file in which it is declared.

#include <stdio.h>

/* 函数声明 */
void func1(void);

static int count=10;        /* 全局变量 - static 是默认的 */

int main()
{
 while (count--) {
     func1();
 }
 return 0;
}

void func1(void)
{
/* 'thingy' 是 'func1' 的局部变量 - 只初始化一次
* 每次调用函数 'func1' 'thingy' 值不会被重置。
*/                
 static int thingy=5;
 thingy++;
 printf(" thingy 为 %d , count 为 %d\n", thingy, count);
}

Variables defined outside the function default to static global variables, while static variables need to be defined internally and initialized every time they are called.

# 加static
 thingy 为 6 , count 为 9
 thingy 为 7 , count 为 8
 thingy 为 8 , count 为 7
 thingy 为 9 , count 为 6
 thingy 为 10 , count 为 5
 thingy 为 11 , count 为 4
 thingy 为 12 , count 为 3
 thingy 为 13 , count 为 2
 thingy 为 14 , count 为 1
 thingy 为 15 , count 为 0
 # 不加static
 thingy 为 6 , count 为 9
 thingy 为 6 , count 为 8
 thingy 为 6 , count 为 7
 thingy 为 6 , count 为 6
 thingy 为 6 , count 为 5
 thingy 为 6 , count 为 4
 thingy 为 6 , count 为 3
 thingy 为 6 , count 为 2
 thingy 为 6 , count 为 1
 thingy 为 6 , count 为 0

extern storage class

The extern storage class is used to define global variables or functions declared in other files.
When the extern keyword is used, no storage space is allocated for the variable, but the compiler is simply instructed that the variable is defined in another file.
When you have multiple files and define a global variable or function that can be used in other files, you can use extern in other files to get a reference to the defined variable or function.
main.c

#include <stdio.h>
 
int count ;
extern void write_extern();
 
int main()
{
   count = 5;
   write_extern();
}

support.c

#include <stdio.h>
 
extern int count;
 
void write_extern(void)
{
   printf("count is %d\n", count);
}

Output:

count is 5

Guess you like

Origin blog.csdn.net/weixin_45636780/article/details/132008044