C language growing parameter list

xiaoxiao2021-03-06  55

Establishing a number of parameters is possible. If the standard library function Printf, its parameters are not fixed. Although printf must use a string as its first parameter, it can receive additional parameters of any number. The prototype of the Printf function is: int Printf (const char * format, ...); omitting number (...) in this function prototype indicates that this function receives any type of parameters. Note that the omitted number must be placed at the end of the parameter list. The macro definition in stdarg.h in the leader (see below) class is used to establish a list of bell parameters. A function average of the number of variable parameters is given in the demo program given below. The first parameter of the function AVERAGE is the number of data to be evaluable. ----------------------------------- ---------------------------- | Types and macros defined in head file stdarg.h | ----- -------------------------------------------------- -------------------- | Logo | Explanation | ---------- ----------- -------------------------------------------------- ----- | VA_LIST | The information used to save VA_START, VA_END is required. In order to access the parameters in the beam parameter column | | | must declare an object of the VA_List type | -------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- | VA_START | The macro used before the parameter in the viable parameter list, which initializes the objects declared with VA_LIST, and the initialization result is for macro VA_ARG and VA_END. | ---------- ----------------------------------- ----------------------------- | VA_ARG | Expand into an expression of the macro, the expression has the list of variable length parameters The next parameter | | | | value and type. Each time you call VA_ARG to modify an object declared with VA_LIST, so that the | | | object points to the next parameter in the parameter list. | ---------- ----------------------------------- ----------------------------- | VA_END | This macro program can use macro VA_START applications from the variable length parameter list Normal return | ---------- --------------------------------- ------------------------------ Function Average uses all the months and macros in the header file stdarg.h. Macro VA_START, VA_ARG and VA_END use the VA_List type object AP processing function Avergage's viable parameter list. The function initializes the object AP used by VA_ARG and VA_END with VA_START. Macro VA_START has two parameters, one is an object AP, and the other is the identifier of the rightmost parameter before the varying length parameter list (I, VA_START use i determines the starting position of the beat of length parameter list) . Then, the function AVERGE repeatedly adds the parameters in the varying parameter list to the variable Total, and the Total value is retrieved from the vitious VA_ARG from the viable parameter list.

Macro VA_ARG has two parameters, one is an object AP, and the other is the type of value to be received from the parameter list (this example is Double), which returns the value of the parameter. Macro VA_END is only a parameter such as an object AP. Function average Macro VA_END makes the program from Average to Main. Finally, the function calculation average returns to main. Common programming errors is to put the omitted number in the middle of the function parameter list. The omitted number can only be placed in the finals of the parameter list. So how do the function printf and scanf know how macro VA_ARG uses each time? The answer is that Printf and Scanf are the type of the next parameter to be processed by the format conversion in the scan format control string. Attachment test: Varparam.c, Redhat Linux 9.0 test passes. # include # include

Double average (int, ...); main () {double w = 37.5, x = 22.5, y = 1.7, z = 10.2; Printf ("w =% .1f / nx =% .1f / ny =%. 1F / NZ =% .1f / n ", w, x, y, z); Printf (" The average of w and x is:% f.1 / n ", Average (2, w, x)); Printf ("THE AVERAGE OF W, X and Y IS:% F.1 / N", Average (3, W, X, Y)); Printf ("The Average of W, X, Y and Z IS:% F. 1 / N ", Average (4, W, X, Y, Z));} Double Average (INT I, ...) {Double Total = 0; INT J; VA_LIST AP; VA_START (AP, I); for (j = 1; j <= i; j ) Total = VA_ARG (AP, Double); VA_END (AP); RETURN TOTAL / I;} Runout output: w = 37.5 x = 22.5 y = 1.7 z = 10.2 The average of w and x is: 30.000000.1 The average of w, x and y is: 20.566667.1 The average of w, x, y And z is: 17.975000.1

转载请注明原文地址:https://www.9cbs.com/read-113387.html

New Post(0)