获取最小值和最大值

数组和集合 / 数组

int numbers[] = {11, 2, 5, 7, 3};
size_t count = sizeof numbers / sizeof numbers[0];

//C 没有数组的 min 和 max:一趟遍历即可同时求出
//(空数组两者都没有,应先检查 count)
int minValue = numbers[0];
int maxValue = numbers[0];
for (size_t i = 1; i < count; i++) {
    if (numbers[i] < minValue) {
        minValue = numbers[i];
    }
    if (numbers[i] > maxValue) {
        maxValue = numbers[i];
    }
}
//minValue is 2
//maxValue is 11

printf("minValue is %d\n", minValue);
printf("maxValue is %d\n", maxValue);