尋找任意數目參數的最大值

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   on   

原文:尋找任意數目參數的最大值

《C和指標》第7章第4道編程題:

編寫一個名叫max_list的函數,它用於檢查任意數目的整型參數並返回它們中的最大值。參數列表必須以一個負值結尾,提示列表的結束。

 1 /* 2 ** 尋找任意數目的整型參數中的最大值 3 */ 4  5 #include <stdio.h> 6 /* 7 ** 要實現可變參數列表,需要包含stdarg.h檔案 8 ** stdarg.h中聲明了va_list, va_start, va_arg 和 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 ** 接受任意個正整數,返回最大值22 ** 參數列表必須以負值結尾,提示列表的結束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     ** 準備訪問可變參數34     */35     va_start( val, n );36     37     /*38     ** 取出可變列表中的值39     ** 負值提示列表結束40     */41     while( ( current = va_arg( val, int ) ) >= 0 )42     {    43         if( max < current )44             max = current;45     }46     47     /*48     ** 完成處理可變列表49     */50     va_end( val );51     52     return max;53 }

 

尋找任意數目參數的最大值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.