C语言初学者编程:使用C语言比较两个数的大小

2023-02-11 320 0

比较两数大小
1.利用if else输出较大值

#include<stdio.h>

int main()

{

int a=0,b=0 ;

scanf("%d%d", &a, &b);
if (a>b)

printf("%d", a);

else if (a<b)

printf("%d", b);

else

printf("相等\n");
system("pause");//防止窗口闪退
return 0;

}
2.直接利用比较运算符打印两个数的较大值

#include<stdio.h>
int main()

{

int a, b;
scanf("%d%d", &a, &b);
printf("%d", a > b ? a : b);
return 0;
system("pause");
}
3.用一个max函数比较两数大小

#include<stdio.h>
int max(int x, int y)//定义一个函数max
{
int c;
c = x > y ? x : y;
return c;
}
int main()
{

int a, b;
scanf("%d%d", &a, &b);
printf("%d\n",max(a,b));//调用max函数
system("pause");
return 0;

}
4.宏定义比较大小

#include<stdio.h>
#define MAX(a,b)((a) > (b) ? (a) : (b))//宏定义
int main()
{

int a, b;
scanf("%d%d", &a, &b);
printf("%d\n",MAX(a,b));//调用max函数
system("pause");
return 0;

}


相关文章

大道至简
如何在WordPress网站文章中不用插件直接引用插入哔哩哔哩bilibili视频
记录下孩子的荣誉
阿里云学生领300元无门槛券(全日制、非全日制学生均可领取)
在python中用ai写了一个键盘鼠标次数统计软件
小米手环可以开通重庆公交卡

发布评论