一:全局变量
#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;
}
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/77c6a7efce1b9d160e873659f1deb48f8c546442?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
二:指针
#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;
}
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/4610b912c8fcc3ceec7e650b9045d688d43f204f?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)