在线编译器 C

#include <stdio.h> int main() { int n; scanf("%d", &n); int max, min; for (int i = 0; i < n; i++) { int num; scanf("%d", &num); if (i == 0) { max = num; min = num; } else { if (num > max) max = num; if (num < min) min = num; } } printf("%d\n", max-min); return 0; }
This program reads an integer `n`, then reads `n` more integers, finds the maximum and minimum among them, and prints the difference (max - min). It initializes `max` and `min` with the first number, then updates them as it reads the rest.

- The variables `max` and `min` are not initialized before the loop. If `n` is 0, the loop body never runs, and the program will use uninitialized values in the final `printf`, leading to undefined behavior. Consider what should happen when no numbers are entered.
- The `scanf` calls do not check the return value. If the input is invalid or ends early, the program may behave unexpectedly. For a robust program, you could verify that `scanf` successfully read the expected number of items.