33. Recognize the true face of a function
Description and definition: 1. In the programStatementIt can be understood as pre-telling the existence of compiler entities, such as variables and functions.
2. In the programDefinitionClearly indicate the meaning of the compiler entity
Function parameters: 1. function parameters are essentially the same as local variables and are allocated space on the stack.
2. the initial value of the function parameter is the real parameter used in the function call.
int f(int i,int j){ printf("%d,%d\n",i,j);}int main(){ int k = 1; f(k,k++); printf("%d\n",k); return 0}
GCC output:, 2 when K ++ is passed to the function parameters, K is first saved to a stack with the Temporary Variable temp. After settlement, the temp is directly transferred to the function, so it is still 1
But K is passed to the function directly in the memory, so it is 2
Resolution: The order in which function parameters are evaluated depends on the implementation of the compiler.
In C language, the order in which most operators evaluate their operands depends on the implementation of the compiler.For example, int I = f () * g (); the call sequence of F and G depends on the implementation of the compiler.
Sequence in the program: Refers to the last time when the value is modified during execution. When the Program reaches the order point, all the previous operations must be reflected in the subsequent access.
1. When each complete expression ends
2. &, | ,? :, And after each calculation object of the comma expression is calculated
3. After the evaluation of all the actual parameters in the function call is completed ()
#include <stdio.h>int main(){ int k = 2; int a = 1; k = k++ + k++; printf("k = %d\n", k); if( a-- && a ) { printf("a = %d\n", a); } return 0;}
Output: K = 6; k = K ++; settlement point: ";", so the settlement is done at the; position. The addition has been completed. K = 2 + 2, and then settle K, K ++, K ++, So k = 6;
The C language specification of this Settlement point is not clearly defined and falls within the scope of the compiler's self-judgment. It can be said that there is no correct answer
34. variable parameters and Macro Analysis
Variable parameters: the implementation of variable parameter functions depends on stdarg. h. The va_list variables and va_start, va_end, and va_arg variables can be used together to access the parameter values.
Example:
#include <stdio.h>#include <stdarg.h>float average(int n, ...){ va_list args; int i = 0; float sum = 0; va_start(args, n); for(i=0; i<n; i++) { sum += va_arg(args, int); } va_end(args); return sum / n;}int main(){ printf("%f\n", average(5, 1, 2, 3, 4, 5)); printf("%f\n", average(4, 1, 2, 3, 4)); return 0;}
Restrictions on variable parameters: 1. variable parameters must be accessed one by one from start to end in order, and the parameter values in the parameter list cannot be accessed directly.
2. At least one named parameter must exist in the parameter list.
3. The variable parameter macro cannot determine the type of an existing parameter.
4. The variable parameter macro cannot determine the actual type of the parameter.
Warning:If an error type is specified in va_arg, the result is unpredictable.
35. function call behavior
Activity record: 1. Temporary Variable field: used to store the value of temporary variables, such as the intermediate result of K ++
2. Local variable field: used to store local variables in the current execution of the Function
3. Machine Status field: used to save information about the machine status before calling a function, including the current value and return address of various registers.
4. Real parameter number field: used to save the real parameter information of a function
5. Return Value Range: Save the return value for the caller's Function
Parameter import Stack: the calculation order of function parameters is dependent.Compiler implementationThe parameter stack-to-stack sequence depends on the call conventions.
Call Convention: describes how parameters are transmitted to the stack space and who maintains the stack space.
Parameter transfer sequence: Stack entry from right to left ----- _ stdcall ,__ cdecl ,__ thiscall
Stack from left to right ----- _ pascall ,__ fastcall
Note: All the above are modifier modifiers.
Call Stack cleanup: 1. The caller clears the stack; 2. The consumer clears the stack after the function is returned.
Conclusion: 1. Function calling is the core mechanism of C language.
2. The activity record stores the function call and all required information returned.
3. Call Conventions are the call protocols between callers and callers. They are often used between library functions compiled by Different developers.
36. Function recursion and function design skills
Recursive Function: 1. Recursive point-call itself with different parameters; 2. Exit-no recursive call
Function design skills:1. Do not use global variables in functions. Try to make functions an independent function module in a sense.
2. The parameter name must reflect the meaning of the Parameter
3. If the parameter is a pointer and only used as an input parameter, you should add const before the type to prevent the pointer from being accidentally modified in the function body.
Void str_copy (char * str_dest, const char * str_src)
4. Do not omit the type of the returned value. If the function does not return the value, it should be declared as void.
5. Check the parameter validity at the entrance of the function body, and check the pointer.
6. The statement cannot return a pointer to "stack memory" because the function body is automatically destroyed when it ends.
7. The size of the function body should be small and should be controlled within 80 lines of code as much as possible.
8. The same input should generate the same output, and try to avoid the function having a memory function.
9. avoid having too many parameters in the function. Try to keep the parameter within four.
10. Sometimes the function does not need to return values, but to increase flexibility, such as supporting chained expressions, you can add return values.
char s[64];int len = strlen(strcpy(s,"android"));
Note: getchar () returns the int type and cannot be used to determine the return value, because the int type includes the EOF definition.