The first sentence of the main function defines three int (integer) variables A, I, K and A, and their values are initialized to 4;
The second sentence is a cycle, and the value of I is defined as 0. When the value of I is less than 2, the loop condition passes, and the value of I will increase by 1 every time the loop is repeated. Therefore, the loop will be executed twice.
In the loop body, the statement k = f (a++) is included; According to the nature of the post-incrementer, the post-incrementer will use the value of the variable first, and then increment the variable after using it. Therefore, the value passed into the function is.
Enter the function, and the value of a in f(int a) is the passed-in value, which is 4. Analyze f () function sentence by sentence;
In the first sentence, the value of integer variable B is defined as 0, and the lifetime and scope of the variable are this function.
In the second sentence, the value of static integer variable C is defined as 9, and the scope of the variable is this function, but its lifetime is the whole file.
In the third sentence, because the post-increment operator is used, the value is used first and then increased, so the value of c will increase after being assigned to A, and then b++. At the end of this statement, the values of c and b are increased by 1, the value of c becomes 10, and the value of b becomes 1.
In the fourth sentence, return takes the value of variable A as the return value.
Go back to the second sentence of the main function and change the value of k to the return value of the f () function, that is, the value of a in the f () function. It should be noted that passing in the value of the function by value does not change the value itself, but is equivalent to copying a copy of the function for use.
After executing this statement, the value of variable A in the main function is increased by 1.
At this point, the first loop of the for loop ends and enters the second loop. Similarly, the value of a was passed in, which has been incremented once before, so now its value is 5. Reenter into the f () function.
In the f () function, because the lifetime of other variables are functions, at the end of the last f () function, variable B is destroyed, so after re-entering the variable, the value of variable B changes back to 0. However, since the life cycle of C has not ended and the value of C remains unchanged, the value of C is still 10 after re-entering the function. The value of c is assigned to a, so the statement that returns a is being executed; The value of time variable A is 10, which means that the return value of the function is 10. After returning, assign this value to k, so that the value of k becomes 10. End of the loop, jump out of the loop.
In the third sentence, execute the printf () function and the output value on the screen is 10.
The fourth sentence, return 0; ? The program is over.
The main difficulty of this topic lies in the determination of variable lifetime, that is, the understanding and mastery of static keywords, ignoring the principle that variables with file lifetime in functions are worth changing when used. At the same time, the post-increment operator is also one of the reasons for the confusion.