【发布时间】:2021-06-04 07:46:39
【问题描述】:
#include<stdio.h>
int CompareNumber(int n1,int n2,int n3 )
{
int a = (n1 < n2) ? n2 : n1;
int b = (a < n3) ? n3 : a;
int c = (n1 > n2) ? n2 : n1;
int d = (c > n3) ? n3 : c;
printf("biggest %d smallest %d", a, d);
return 0;
}
int main(void)
{
int a, b, c;
scanf_s("%d,%d,%d", &a, &b, &c);
CompareNumber(a, b, c);
return 0;
}
当我输入 5,6,7 时,这段代码打印出 5 表示最大的数字,而一些无意义的数字表示最小的数字
【问题讨论】:
-
更好的变量名会有所帮助——最大的数字是
b,而不是a。您是否在输入中输入了逗号并验证了输入是否正确读取,可能是通过回显输入? -
b已定义但未在CompareNumber中使用。显示你得到的确切输出。 -
omg 非常感谢我没有输入逗号...
标签: c max min conditional-operator function-definition