
正文
c语言比两数大小调用函数 c语言比较两个数的大小函数
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
c语言比较两个数大小
这个题目你用c语言比两数大小调用函数的是双精度的整型doublec语言比两数大小调用函数,所以输入输出的类型限定符为“%lf”而不用“%f”c语言比两数大小调用函数,因为你要使用“%f”时就是把双精度的数强制转化为单精度的,可能会造成精度损失,出现一个意外的输出。
当然了,你也可以这样改,把double改为float
最后输出的时候你如果不想要那么多小数,就用"%.2lf"意思是保留2位小数,用float时你用“%.2f”
# include stdio.h
double min (double x, double y);
int main (void)
{
double a, b;
double c;
printf ("输入两个需要比较的数:\n");
while ((scanf ("%lf%lf", a, b)) == 2)
{
c = min (a, b);
printf ("最小的数为%.2f\n", c);
}
return 0;
}
double min (double x, double y)
{
if (x y)
return y;
else
return x;
}
相关问答
Q1: c语言如何用函数比较两数大小
用max函数就行
#include "stdio.h"
int max(int,int);
main()
{
int x,y,z;
printf("input two number:\n");
scanf("%d%d",x,y);
z=max(x,y);
printf("%d",z);
}
int max(int a,int b)
{
if(ab)
return a;
else
return b;
}
Q2: 怎样编写一个C语言程序比较两个数的大小?
方法一:直接输入数据比大小
#include iostream.h
int main(void)
{
float x,y,z;
cout"请输入需比较的数"endl;
cinxy;
if(xy)z=x;
else z=y;
cout"最大值为"zendl;
return 0;
}
方法二:调用函数比较大小
#include iostream.h
int main()
{
float max(float x, float y ); //函数声明既可以在此处,也可以在函数外。
//如果一个函数被多个函数调用时一般选择在函数外做声明
//谭浩强P97
float i,j,m;
cout"输数"endl;
cinij;
m=max(i,j);
cout"最大值为"mendl;
return 0;
}
float max(float x,float y) //比较函数
{
float z;
if(xy) z=x;
else z=y;
return z;
}
//声明函数float max(float x, float y );就是要调用的函数最后再加一个分号;
很久以前的学习记录,好容易才翻出来
我个人建议,你都看看方法二,虽然看上去行数多些,但是你往后学习函数调用要很熟练才好
Q3: C语言程序,要求两个数通过函数调用比较两个数的大小,并把大数返回给主调函数,输出这个数。
/*#include
stdio.h
void
main()
{
int
max(int
*p1,int
*p2);
int
a,b,c;
int
*p1,*p2,*p3;
scanf("%d
%d",a,b);
p1=a;
p2=b;
p3=c;
if(ab)
/*这样其实只能比较一开始输入的第一个值大于第二个值。。*/
{
/*注意加了挂号*/
max(p1,p2);
c=max(p1,p2);
/*原先没有进行比较,c总是等于第一个数*/
}
printf("%d",*p3);
}
int
max(int
*p1,int
*p2)
{
int
temp;
temp=*p1;
return(temp);
}
*/
#include
stdio.h
void
main()
{
int
max(int
*p1,int
*p2);
int
a,b,c;
int
*p1,*p2,*p3;
scanf("%d
%d",a,b);
p1=a;
p2=b;
p3=c;
if(ab)
/*当输入第一个值小于第二个输入的值时*/
c=max(p1,p2);
else
c=b;
printf("%d",*p3);
}
int
max(int
*p1,int
*p2)
{
int
temp;
temp=*p1;
return
temp;
}
Q4: 用C语言编写一个比较两个整数大小的函数,调用该函数比较从键盘输入的两个整数,输
#include stdio.h
// 返回x, y中较大者
int max(int x, int y)
{
return x y ? x : y;
}
int main()
{
int a, b;
printf("input two number:");
scanf("%d %d", a, b);
printf("max = %d", max(a, b));
return 0;
}
关于c语言比两数大小调用函数和c语言比较两个数的大小函数的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







