壹:全局變量
#include?<stdio.h>int?a,b,c;?//定義三個全局變量
void?sort()?//不需要參數
{
int?t;
if(a>b)
{
t=a;a=b;b=t;
}
if(b>c)
{
t=b;b=c;c=t;
}
if(a>b)
{
t=a;a=b;b=t;
}
}
int?main()
{
printf("輸入:");
scanf("%d%d%d",&a,&b,&c);
sort();
printf("排序:%d?<?%d?<?%d\n",a,b,c);
return?0;
}
二:指針
#include?<stdio.h>void?sort(int?*a,?int?*b,?int?*c)?//參數傳遞方式:地址傳遞
{?
int?t; if(*a>*b)? { t=*a;*a=*b;*b=t; } if(*b>*c) { t=*b;*b=*c;*c=t; } if(*a>*b)? { t=*a;*a=*b;*b=t; }}
int?main()
{
int?a,b,c;
printf("輸入:");
scanf("%d%d%d",&a,&b,&c);
sort(&a,&b,&c);//把地址作為參數
printf("排序:%d?<?%d?<?%d\n",a,b,c);
return?0;
}