c语言比较分数大小
时间: 2023-07-02 15:05:56 浏览: 139
在C语言中,可以使用`if`语句或者三目运算符来比较分数大小。以下是两种方法的示例代码:
使用if语句:
```c
#include <stdio.h>
int main() {
float score1 = 76.5;
float score2 = 85.5;
if (score1 > score2) {
printf("score1 is greater than score2\n");
} else if (score1 < score2) {
printf("score2 is greater than score1\n");
} else {
printf("score1 is equal to score2\n");
}
return 0;
}
```
使用三目运算符:
```c
#include <stdio.h>
int main() {
float score1 = 76.5;
float score2 = 85.5;
char* result = (score1 > score2) ? "score1 is greater than score2" : (score1 < score2 ? "score2 is greater than score1" : "score1 is equal to score2");
printf("%s\n", result);
return 0;
}
```
以上两种方法都可以实现比较分数大小的功能。
阅读全文