Detailed explanation of array 2

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.


Preface

Hello everyone, today we will continue to explain the remaining knowledge points of arrays from the last chapter. If you want to review the previous section, please click the linkArray Detailed Explanation 1


1. Declaration and definition of functions

1.1 Single file

Generally when we use a function, we write the function directly and use it.
For example: We need to write a function to determine whether a year is a leap year.
Code demonstration: Insert image description here
In this scenario, the function is defined before the function is called, so there is no problem.

Then if we put the function definition after the function call, as follows:
Insert image description here
This is because when the C language compiler compiles the source code, it scans from the first line downwards. When it encounters the is_leap_year function call, it does not find the definition of is_leap_year in front of it, so it reports the above warning.
How to solve this problem? That is, declare the function is_leap_year before calling the function. When declaring the function, you only need to explain clearly:function name, function return type and function parameters.

For example, code demonstration:
Insert image description here
The function call must be satisfactory, declare it first and then use it;
The definition of the function is also a special declaration, so it is okay if the function definition is placed before the call.

1.2 Multiple files

Generally, when we write code in an enterprise, there may be a lot of code, and we will not put all the code in one file; we often split the code into multiple files according to the function of the program.
Generally, function declaration and type declaration are placed in the header file (.h) , The implementation of the function is placed in the original file (.c) file.
As below:
add.c

//函数的定义
int Add(int x, int y)
{
    
    
 return x+y;
}

add.h

//函数的声明
int Add(int x, int y);

test.c

int main()
{
    
    
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);

	int c = Add(a, b);
	printf("%d\n", c);
	return 0;
}

Run result:Insert image description here
With the understanding of function declaration and function definition, it is more convenient for us to write code.

1.3static and extern

static and extern are both keywords in C language.
static means static and can be used for:

• Modify local variables
• Modify global variables
• Modify functions

extern is used to declare external symbols.
Before explaining static and extern, let’s talk about scope and life cycle.
Scope (scope) is a programming concept. Generally speaking, the names used in a piece of program code are not always valid (available). The scope of code that limits the availability of this name is the scope of this name.

  1. The scope of a local variable is the local scope where the variable is located.
  2. The scope of global variables is the entire project.

Life cycle refers to the period of time between the creation of a variable (application for memory) and the destruction of the variable (reclaiming memory).

  1. The life cycle of local variables is: the life cycle begins when entering the scope and ends when exiting the scope.
  2. The life cycle of global variables is: the life cycle of the entire program.

1.3.1static Modify local variables:

Insert image description here
Insert image description here
Compare the effects of code 1 and code 2 to understand the meaning of static modification of local variables.

Code 1 The local variable i in the test function of is created every time the test function is entered (the life cycle begins) and assigned a value of 0, then ++, and then printed , the variable life cycle will end (memory is released) when exiting the function.

In Code 2, we can see from the output results that the value of i has a cumulative effect. In fact, after i in the test function is created, it will not work when exiting the function. If it is destroyed, the variable will not be re-created when you re-enter the function, and the calculation will continue with the last accumulated value.

ConclusionStatic modification of local variables changes the life cycle of the variable. The essence of the life cycle change is to change the storage type of the variable. Originally, a local variable was stored in the stack area of ​​​​the memory, but it was stored in the static area after being modified by static. Static variables stored in the static area are the same as global variables. The life cycle is the same as the life cycle of the program. Only when the program ends, the variables are destroyed and the memory is recycled. But the scope remains unchanged.

Insert image description here
Usage suggestions: After a variable leaves the function in the future, if we still want to retain the value, we can use static modification when we enter the function next time to continue using it.

1.3.2static modification of global variables

daigo1
add.c

int g_val = 2018;

test.c

#include <stdio.h>
extern int g_val;
int main()
{
    
    
 printf("%d\n", g_val);
 return 0;
}

daigo2
add.c

static int g_val = 2018;

test.c

#include <stdio.h>
extern int g_val;
int main()
{
    
    
 printf("%d\n", g_val);
 return 0;
}

extern is used to declare external symbols. If a global symbol is defined in file A and you want to use it in file B, you can use extern to declare it and then use it.
Code 1 is normal, but code 2 will cause a connectivity error during compilation.

in conclusion:
A global variable is modified static, so that the global variable can only be used in this source file and cannot be used in other source files. The essential reason is that global variables have external link attributes by default. If you want to use them in external files, you can use them as long as they are declared appropriately; but after the global variables are modified by static, the external link attributes become internal link attributes and can only be used. It is used inside the source file where it is located. Other source files, even if declared, cannot be used normally.
Usage suggestions: If a global variable is only used within the source file where it is located and does not want to be discovered by other files, you can use static modification.

1.3.3static modified function

Yo 1:
add.c

int Add(int x, int y)
{
    
    
 return x+y;
}

test.c

#include <stdio.h>
extern int Add(int x, int y);
int main()
{
    
    
 printf("%d\n", Add(2, 3));
 return 0;
}

Yo2:
add.c

static int Add(int x, int y)
{
    
    
 return x+y;
}

test.c

#include <stdio.h>
extern int Add(int x, int y);
int main()
{
    
    
 printf("%d\n", Add(2, 3));
 return 0;
}

Code 1 can run normally, but code 2 has a link error.
In fact, static modified functions are exactly the same as static modified global variables. A function can be used in the entire project. After being static modified, it can only be used within this file, and other files cannot be linked and used normally. The essence is that functions have external link attributes by default, so that functions can be used throughout the entire project as long as they are properly declared. However, after being modified by static, it becomes an internal link attribute, so that the function can only be used within the source file where it is located.

Usage suggestions: If a function only wants to be used within the source file where it is located and does not want to be used by other source files, it can be modified with static.

Summarize

Okay, this blog ends here. If you have a better point of view, please leave a message in time. I will watch it carefully and learn from it.
If you don’t accumulate steps, you can’t reach a thousand miles; if you don’t accumulate small streams, you can’t become a river or sea.

Guess you like

Origin blog.csdn.net/2301_79585944/article/details/133808021