Directory
4.1 Basic knowledge of functions 4.2 function external variable 4.4 scope rule returning a non-integer value header file static variable Register variable program block structure Initializes a 4.10 Recursive 4.11 C preprocessor file that contains a macro substitution condition that contains
4.1 Basic knowledge of functions
function can decompose large computational tasks into several smaller tasks C language in the design of the efficiency of the function and ease of use of the two factors program can be seen as a variable definition and function definition of the aggregate function between the communication can be through parameters, function return values and external variables the order in which functions appear in the source file can be arbitrary, as long as a function is guaranteed not to be separated into multiple files
4.3 External variables
C language programs can be seen as a series of external objects, these external objects may be variables or functions external and internal is relative, internal is used to describe the function parameters defined inside the function and variables outside the variables defined in the function, So it can be used in many functions because the C language does not allow other functions to be defined in a function, so the function itself is "external" All references to external variables by the same name are actually references to the use of external variables of the same object as well as their greater scope and longer lifetime automatic variables than internal variables can only be used inside the function, starting from the time when the function is called to exist, Variables will also disappear when the function exits the external variables are permanent, and their values remain constant between the time of the function call and the next function call
4.4 Scope Rules
The functions that make up the C language program can be compiled separately from the external variables. A program can be stored in several files, the original compiled function can be loaded from the library name scope refers to the program can use the name of the part of the function switch declaration of the automatic variables, Its scope is the same as the argument for declaring the variable name in a function with the same name in a different function that has no relation function between each local variable, and actually can buy it as a local variable the scope of the external variable or function begins where it is declared, Ends at the end of the file to which it resides if you want to use the variable before the definition of an external variable, or if the definition of an external variable is not in the same source file as the use of the variable, it is important to use extern to strictly differentiate the declaration and definition of an external variable in the corresponding variable declaration. The variable declaration describes the properties of the variable, and the variable definition will also cause the memory to be allocated in all source files of a source program, an external variable can be defined only once in a file, and other files may be accessed through an extern declaration that must specify the length of the array in the definition of the external variable. But the extern declaration does not have to specify the length of the array the initialization of external variables can only appear in the definition
4.6 Static variables
Using the static declaration to qualify external variables and functions, you can limit the scope of the object that is declared later to the remainder of the compiled source file by static to qualify the external object, so that static declarations outside of the purpose of hiding external objects are usually used for variables. It can also be used to declare a function, which is generally accessible globally. However, if the function is declared as a static type, the function name is visible in addition to the file where the function declaration resides. Many other files cannot be accessed static you can also declare internal variables. The internal variable of the static type is a local variable of a particular function and can only be used in that function, but unlike an automatic variable, it persists regardless of whether its function is called or not, and does not, like an automatic variable, exists and disappears as the function is called and exited. The internal variable of the static type is a variable that can only be used in a particular function but always occupies the storage space
4.7 Register variables
The Register declaration tells the compiler that the variable it declares is used more frequently in the program its idea is to place the register variable in the Register of the machine, which makes the program smaller, Execution is faster. However, the compiler can ignore this option. The Register declaration applies only to automatic variables and formal parameters of functions. The actual use of the underlying hardware environment has some limitations on the use of register variables. Only a few variables in each function can be kept in registers, Only certain types of variables are allowed However, excessive register declarations do not declare harm because the compiler can ignore excessive or unsupported register variable declarations. In addition, regardless of whether the register variable is actually not in the storage register, its address is inaccessible
4.8 Program Block structure
The declaration of a variable can be followed immediately after the curly brace at the beginning of the function and immediately after the other opening curly brace that marks the beginning of the compound statement.
Variables declared in this way can hide variables with the same name as the block, and there is no relationship between them, and the right curly brace that matches the left curly brace is present until it appears.
4.9 initialization
In the case of no explicit initialization, the external and static variables are initialized to 0, while the initial values of the automatic and register variables are undefined (i.e., the initial value is useless information) for external variables and static variables, the initialization expression must be a constant expression and only initialized once ( It is conceptually initialized before the program starts executing) for automatic variables and register variables, the initialization expression may not be a constant expression for automatic variables and register variables, for both the auto variable and the registers variable: the expression can contain values that have been defined before any of this expression
4.10 Recursion
A function in C can be called recursively, that is, a function can call itself recursively, either directly or indirectly, and each invocation will get a new set of automatic variables that is different from the previous set of automatic variables recursive and does not save memory overhead. Because the recursive call process must be maintained somewhere where a stored processing value is not executed quickly, the recursive code is more compact and easier to write and understand than the corresponding non-recursive code
4.11 C Preprocessor
The C language provides some language functionality through the processor conceptually, the preprocessor is the first step to be performed separately during compilation. Two most commonly used preprocessor directives are: #include directives and # define Directives
The 4.11.1 file contains the # include "file name" #include < file name > If the file name is enclosed in quotation marks, it is found in the location where the source file is located, if no files are located at that location, or if the file name is enclosed in angle brackets <>. The file will be found according to the appropriate rule, which is related to the specific implementation
4.11.2 macro replaces a # define name replace text follow so the place where the name is marked will be replaced with the replacement text the name in the # define directive is named the same as the variable name, and the replacement text can be any string typically the # define directive takes one line, and the replacement text is # Define the end of the command line so that the remainder of the content can also be a longer macro substitution into several lines, you need to be continued at the end of the line with a backslash # # directive definition of the name of the scope from its definition point, to the ends of the compiled source file The macro definition can take parameters, This allows you to use different alternate text for different macros.
4.11.3 conditions include #ifndef hdr#define hdr#endif
C Programming language Note (iv) function and program structure