Search for the maximum value of any number of parameters.
Chapter 2 of C and pointers: 7th programming questions:
Compile a function named max_list, which is used to check Integer Parameters of any number and return the maximum values among them. The parameter list must end with a negative value, prompting the end of the list.
1/* 2 ** search for the maximum value of any integer parameter 3 */4 5 # include <stdio. h> 6/* 7 ** to implement a variable parameter list, you must include stdarg. h file 8 ** stdarg. h declares va_list, va_start, va_arg, and va_end 9 */10 # include <stdarg. h> 11 12 int max_list (int n ,...); 13 14 int 15 main () 16 {17 printf ("% d", max_list (10, 23, 89, 56, 83, 91,100,-1 )); 18} 19 20/* 21 ** accept any positive integer, return the maximum value 22 ** parameter list must end with a negative value, the end of the prompt list is 23 */24 int 25 max_list (int n ,...) 26 {27 va_list val; 28 int max = 0; 29 int I; 30 int current; 31 32/* 33 ** ready to access Variable Parameter 34 */35 va_start (val, n); 36 37/* 38 ** retrieve the value 39 from the Variable list ** the negative value indicates that the list is over 40 */41 while (current = va_arg (val, int)> = 0) 42 {43 if (max <current) 44 max = current; 45} 46 47/* 48 ** complete processing Variable list 49 */50 va_end (val ); 51 52 return max; 53}