function Function: hide operation details, structure more clear, reduce the difficulty of modification;
4.1 Function Basics return value type function name (parameter declaration table) {Declaration and Statement} The order in which the function appears in the source file can be arbitrary; the return value type is omitted, the default Int;return does not have an expression, and execution to the last closing curly brace is returned: "Value that is not a valid return value, but not returned successfully" "It must be useless; A program can be seen as a set of variable definitions and function definitions; a function communicates through parameters, return values, and external variables; 4.2 A function function that returns a non-integral type is in the same source file as the main function that invokes it, and the compiler discovers the error when the type is inconsistent;
Implicit declaration:If a name that has not been declared appears in an expression, followed by an opening parenthesis, the context assumes that it is a function name, the return value is assumed to be int, and its arguments are not assumed;
return(expression); When the value of an expression is returned, it is converted to the function type, which may lose information some compilers warn, so type conversions can be displayed; 4.3 external variable C programs can be seen as a series of
External Objects(including variables and functions); External variables are defined outside the function; functions are not allowed to be defined in other functions, so each function is also "external";?? (It) External variables and functions have the default "
External links"Nature: Even if it comes from a separate compilation of different functions, the same name is referenced by the same object, the function can communicate with the help of external variables, especially when the function needs to share a large amount of information, but also make a large number of data relations between functions, affecting the program structure; We will describe how to define external variables and functions that are used only in one source file, and then discuss how to split the program into multiple source files; external variables
Survival Time: Permanent, two-time function calls remain the same, function-shared "
Stack", can be defined as an external variable, while defining a pointer to save the next idle stack position; 4.4 Scope rule functions and external variables can be compiled separately; A program can be placed in several files; the compiled function can be loaded from the library;
Scope:The part of the name can be used in the program; the scope of the local variable (including the function parameter) is within the function; the outer variable or function scope starts at the declaration, ends at the end of the file where it is to be compiled, and the function calls them without declaring the direct use;
extern:If you want to use it before the external variable definition, or if the definition and use are not in the same source file, the declaration must be forced to use extern;?? External variables
declarations and DefinitionsDistinction: int sp;double Val[max] that is placed outside of all functions, defines an external variable sp and Val, allocates a memory unit, and acts as a declaration for the remainder of the source file; extern int Sp;extern double Val[max] An external variable SP and Val are declared for the remainder of the source file (the array length can be determined elsewhere), but no variables or storage units are created; In all source files of a source program, an external variable can be defined only once in a file, and the other file must be preceded by an extern declaration before it can be accessed; The extern declaration can also be made in the source file where the definition resides, and the external variable initialization can only appear in its definition; If a variable is to be used before the definition, it must be preceded by an extern declaration; 4.5-head file in the actual program, considering that its parts may come from a separate compiled library, Therefore, the source program is often divided into multiple source files; Consider the shared issue of definitions and claims: (IT) focus on each source file share/Public section (macro substitution, function prototype, etc.) in a header file as much as possible, and then include it in each required source file via include "~.h" Large-scale programs may require multiple headers; 4.6 Static variables
ExternalThe declaration of a variable or function is preceded by static: The object scope is scoped to the remainder of the compiled source file and is not visible to other files (the same name does not conflict);
InternalVariable declaration before the static: As the automatic variable can only be used within the function, the difference is that regardless of whether the function is called, it always exists to occupy the storage space, rather than as the automatic variable with the function of the call and exit to exist and disappear ; (It) This function can use the static variable value left by the last call at the next call, and sometimes it is useful (p57); 4.7 Register variable can be added to the register before the declaration of an automatic variable or function parameter used at a higher frequency, and the compiler can put it in a register to speed up The compiler can also ignore this option;
actual underlying hardware conditionRestrictions: Only a small number of certain types of variables per function can be stored in registers, excessive register declarations are harmless, the compiler ignores excessive or unsupported register variable declarations, and regardless of whether the register variable is actually placed in the register, its address is always inaccessible; 4.8 block structure C language cannot define functions in functions , but can be in the function's
defining variables in the program block structureA variable declaration (including initialization) can be immediately followed by any left curly brace that identifies the beginning of a compound statement, and it exists before the right curly brace that matches it, and it has no relation to a variable of the same name outside of the block; 4.9 initialization
do not explicitly initialize:The external and static variables are initialized to 0, and the initial values of the automatic and register variables are undefined (i.e. useless information);
Explicit initialization:External variables and static variables = constant expressions, and are initialized only once before the beginning of the program, and automatic variables and register variables are initialized each time they enter a function or block, and do not need to be constant expressions: they can contain previously defined values and function calls;
initialization of the array:int day[]={31,28,31,30,31,30,31,30,31,30,31}; When the array length is ignored, the number of initialization expressions in curly brackets will be the compiler's length, the length of the array is more than the number, the remaining uninitialized elements will be assigned 0 for external variables, static variables, and automatic variables; an initialization expression cannot be assigned to more than one element at a time. ; You cannot skip the preceding element directly initializing the back element;
character array initialization:Char pattern[]= "Ould", equivalent to char pattern[]={' o ', ' u ', ' l ', ' d ', ' n '}; The length of the array is 5;4.10 recursive function can call itself directly or indirectly;
Quick-line function:void qsort (int v[], int left, int right)//p74 the Qsort function in the standard library can sort objects of any type; recursion does not save storage overhead (a stack that must be maintained somewhere to store processing values), and execution is not fast, but its code is tight Easy to write and understand; it is very convenient to use the data structure of recursive definition such as tree; the 4.11 C preprocessor preprocessor is the first step to be performed separately during compilation;
The file contains:#include "file name" or #include < file name > any of the lines in the source file will be replaced with the content specified by the file name, or "" to locate the file in the location where the source file is located, or to find it if it is not found or <> according to the corresponding rule (specific implementation); Include directives are commonly used to include # define, extern declarations, function prototypes, which in larger programs can help avoid errors, and if a contained file content changes, obviously all source files that depend on it must be recompiled;
macro substitution:
macro definition:#define Name replaces the text name with the same variable, the replacement text can be any string, the replacement text should be a branch, then the end of the line to add \; Scope from the definition point to the beginning of the source file; The macro definition can use the macro definition that appears earlier, and the substitution is useful only for tokens that are not part of a name Macro definitions with parameters, for example: #define MAX (A, B) ((a) > (b)? (A): (B) Use caution: Proper use of parentheses to ensure correct order of calculation, consideration of expressions such as self-amplification and other side effects;
the value of the macro:Functions (such as GETCHAR) are defined as macros to avoid the overhead of calling functions, and #undef names can be used to cancel macro definitions; The formal parameter in the name cannot be replaced with a quoted string and, if necessary, precede the parameter with # in the replacement text;
preprocessing operator # #: If the replacement text in the shape participates in it adjacent, then the parameter is replaced by the actual parameter # #及其前后空白符会被删除, the replacement result will be re-scanned; This method can be used to connect the actual parameters, for example: #define PASTE (front, back) Front # # Back the macro calls paste ( Name, 1) will establish a notation name1; conditions include: #if-#elif-#else-#endif where the expression must be a constant integer expression (and does not contain sizeof, (type), enum amount), can be defined (first name) Expressions (which can be used to avoid repeating a name or file, many files use this method, regardless of the dependencies between each header file); #ifdef与 #ifndef: Specifically testing whether a name is defined, for example: #ifndef hdr#define hdr// Put hdr.h content #endif it is equivalent to #if!define (HDR) #define HDR//put hdr.h content #endif
"C Programming Language (2nd Edition/New Version)" Chapter 4th function and program structure