Source: Find the maximum of any number of parameters
"C and Pointers" 7th Chapter 4th Programming Questions:
Write a function called Max_list, which checks for any number of integer parameters and returns the maximum value in them. The argument list must end with a negative value, prompting the end of the list.
1 /*2 * * Find the maximum value in any number of integer parameters3 */4 5#include <stdio.h>6 /*7 * * To implement a mutable parameter list, you need to include the Stdarg.h file8 * * va_list, Va_start, Va_arg and Va_end are declared in Stdarg.h9 */Ten#include <stdarg.h> One A intMax_list (intn, ...); - - int the Main () - { -printf"%d", Max_list (Ten, at, the, About, the, the, -, -1) ); - } + - /* + * * Accept any positive integer to return the maximum value A * * The parameter list must end with a negative value, indicating the end of the list at */ - int -Max_list (intN, ...) - { - va_list Val; - intMax =0; in inti; - intCurrent ; to + /* - * * Ready to access mutable parameters the */ * Va_start (Val, n); $ Panax Notoginseng /* - * * Remove values from the mutable list the * * Negative tip list End + */ A while(current = Va_arg (Val,int)) >=0 ) the { + if(Max <Current ) -Max =Current ; $ } $ - /* - * * Complete processing of mutable lists the */ - Va_end (val);Wuyi the returnMax; -}
Find the maximum value of any number of parameters