If you need read an entire line, then read an entire line, simple as that. If you google "C read line", you will most probably end up reading the documentation of fgets(). Then you google "C convert string to integer", and you perceive that there exists a function called strtol() in the C standard library. Armed with these two weapons, and applying some logic, you can deduce something like this:
const size_t max_numbers = 1000; // however many
int numbers[max_numbers];
size_t index = 0;
char buf[LINE_MAX];
while (index < max_numbers && fgets(buf, sizeof buf, stdin)) {
char *p = buf;
char *end;
while (index < max_numbers && *p && *p != '\n') {
numbers[index++] = strtol(p, &end, 10);
p = end;
}
}
scanf()instead of making assumptions about its return value. Also, do NOT usescanf(). Usefgets()along withstrtol().scanf()is going to block and wait for more numbers to come. Usefgets()instead to read the whole line, and thensscanf()to extract each number.