max_list(任意整形參數列表)

#include <stdio.h>
#include <stdarg.h>

int max_list( int i,...);
int main()
{
	int a = 0;
	a = max_list( 3, 5, 6,2, -1);
	printf( "%d", a);
	return 0;
}

int max_list(int i , ... )
{
	va_list  ap;
	int     max = 0;
    int		a;

	va_start( ap,i );
	while( ( a = va_arg( ap, int )) > 0 )
	{
		max = max > a ? max : a ;
	}

	va_end( ap );

	return max;
}
自己寫的有錯誤。也不改 了。答案如下:         
/*
** Return the largest value from the argument list.
** negative value.
*/
The list is terminated by a
#include <stdarg.h>
int
max_list( int first_arg, ... )
{
va_list var_arg;
int
max = 0;
/*
** Get the first arg if there is one and save it as the max.
*/
if( first_arg >= 0 ){
int
this_arg;
max = first_arg;
/*
** Get the remaining arguments and save each one if it is
** greater than the current max.
*/
va_start( var_arg, first_arg );
while( ( this_arg = va_arg( var_arg, int ) ) >= 0 )
if( this_arg > max )
max = this_arg;
va_end( var_arg );
}
return max;
}


發佈了47 篇原創文章 · 獲贊 7 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章