We usually handle this kind of thing by first reading the entire line as a string, and then using sscanf to "scan" the numbers from the string. All the scanf functions return a count of the number of fields that they filled.
Code:
#include <stdio.h>
int main()
{
char line[200];
while (fgets(line, sizeof line, stdin) != NULL)
{
int d1, n1, d2, n2;
int nfields = sscanf(line, "%d%d%d%d", &d1, &n1, &d2, &n2);
if (nfields == 2)
{
printf("==> %d %d\n", d1, n1);
}
else if (nfields == 4)
{
printf("==> %d %d %d %d\n", d1, n1, d2, n2);
}
else
{
printf("Only enter 2 or 4 numbers.\nTry again.\n");
}
}
return 0;
}
This allows lines like "1 2 3 4 5 6" where it will only read the first 4 numbers and ignore the rest.
Also like "1 2 hello 3 4" where it will read only the first 2 numbers and ignore the rest of the line.
If detecting bad input like that is important than you need to do a little more.