C Number Comparison Guide
In C language, comparing the size of two numbers can be achieved as follows:
- Use the if statement:
#include <stdio.h>
int main() {
int num1, num2;
printf("请输入两个整数:\n");
scanf("%d %d", &num1, &num2);
if (num1 > num2) {
printf("%d 大于 %d\n", num1, num2);
} else if (num1 < num2) {
printf("%d 小于 %d\n", num1, num2);
} else {
printf("%d 等于 %d\n", num1, num2);
}
return 0;
}
- Using the ternary operator:
#include <stdio.h>
int main() {
int num1, num2;
printf("请输入两个整数:\n");
scanf("%d %d", &num1, &num2);
int max = (num1 > num2) ? num1 : num2;
int min = (num1 < num2) ? num1 : num2;
printf("最大值为:%d\n", max);
printf("最小值为:%d\n", min);
return 0;
}
Both of these methods can be used to compare the sizes of two numbers, so choose the appropriate method based on the specific situation.