Search for the maximum value of any number of parameters.

Source: Internet
Author: User

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}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.