1. Why do we need global variables?
Local variables include: the formal parameters of the function and the parameters defined inside the function. Because the scope of local variables is limited, we need to use the action of "passing address parameters to the function" to change the value of our local variables.
2. The scope of global variables
- Knowledge point: Variables defined outside functions are called external variables. The scope of external variables is below the definition of all external variables in the .c program, so global variables are not necessarily external variables as shown in the figure:
- Verify the scope of global variables through a case: the fun1 function and fun2 function cannot use external variables p and q in the following cases.
#include <stdio.h> void fun1() { printf("fun1:%d\n", p); printf("fun1:%d\n", q); } void fun2() { printf("fun2:%d\n", p); printf("fun2:%d\n", q); } int p = 100; //external variable int q = 90; //external variable int main(int argc, char const *argv[]) { fun1(); fun2(); printf("main:%d\n", p); printf("main:%d\n", q); return 0; }
3. The correct writing position of global variables
- Write at the beginning of the file (before all functions)
#include <stdio.h> void fun1(); void fun2(); int p = 100; //global variable int q = 90; //global variable int main(int argc, char const *argv[]) { fun1(); fun2(); printf("main:%d\n", p); printf("main:%d\n", q); return 0; } void fun1() { printf("fun1:%d\n", p); printf("fun1:%d\n", q); } void fun2() { printf("fun2:%d\n", p); printf("fun2:%d\n", q); }
4. The risk of global variables
- All functions can operate on it, and sharing a variable is error-prone, especially for multi-threaded development. In the above example, we are single-threaded, that is, when fun1 is executed, fun2 will not be executed; but when learning the multi-threading of the operating system application layer, it will involve the sharing of global variable values.
5. Exercises
1. Code experience:
- As of now, there is only one return value of a function, or the method of passing an array to modify the elements in the array, but we have no way to obtain multiple return values of different types from a function. There are three ways to solve this problem:
- Let the name of the structure be used as a function parameter, modify the value of the structure in the function, and then return the address of the structure.
- Directly pass the pointer in the calling function to operate on the addresses of the variables that need to be changed, and change their values.
- At present, only the way of global variables can be used: modify the global variables in the function, and the function itself has a return value.
- The function emphasizes the function, which is the encapsulation of the function, and is finally used by the caller. In exercise 1, if you even write the statement "print the average" in the function, it may be possible, but it is extremely unreasonable , we don't want the function of the function to be too messy, we hope to get something from the function, or modify the parameters we pass in. On the other hand, if the function has too many functions, then you have to write the function name very long.
Exercise 1: There are 10 students in the class, and a function is encapsulated to obtain the average, maximum, and minimum values.
- Ideas:
define global variables max,Indicates the maximum value of the student's grade: int max; define global variables min,Indicates the minimum value of the student's grade: int min; f1. Encapsulates an array average API: float getAverage(int arr[], int len); f1.1 Modify global variables max and min,May wish to order max = min = arr[0]; f1.2 for Loop, the loop variable that represents the subscript i start from 0, <len , enter the loop f1.2.1 Change the loop variable used for summing sum: sum = sum + arr[i]; f1.2.2 judge max Is it less than arr[i] f1.2.2.1 in the case of, then put arr[i]as the maximum: max = arr[i]; f1.2.3 judge min Is it less than arr[i]; f1.2.3.1 in the case of, then put arr[i]as minimum: min = arr[i]; f1.3 return mean with cast: return (float)sum/len; 1. define and initialize an array scores 2. use sizeof operator to get the length of the array len 3. use API1. get the average: aver = getAverage(scores, len); 4. print top score max,Lowest score min,The average score aver
- code:
#include <stdio.h> int max; int min; float getAverage(int arr[], int len); int main(int argc, char const *argv[]) { int scores[] = {66,77,74,75,88,94,90,87,89,83}; float aver; int len = sizeof(scores) / sizeof(scores[0]); aver = getAverage(scores, len); printf("The highest marks for the class are:%d\n minimum score is%d\n average score is:%f\n", max, min, aver); return 0; } float getAverage(int arr[], int len) { int i; int sum = 0; max = min = arr[0]; for(i=0; i<len; i++){ sum = sum + arr[i]; if(max < arr[i]){ max = arr[i]; } if(min > arr[i]){ min = arr[i]; } } return (float)sum/len; }