获取最小值和最大值

数组和集合 / 数组

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

//C has no min and max for arrays: one pass finds both
//(an empty array has neither, check count first)
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);