The return value of scanf is the number of scanned inputs per call. You can compare it with an integer of 10 (or '\ n'), which will stop the loop when scanf actually reads 10 elements. (And for this, there should have been ten qualifiers in the format string.
You can try
while((i<10) && (scanf("%d",a+i)==1)) i++;
You need to program any number of arguments, for example. a
while (fgets(mybuf, ALLOCATED_LENGTH-1, stdin)) { char *p = mybuf; if (*p=='\n') break; // or a better function to test for empty line while (custom_scan_next_integer(&p)) i++; }
where custom_scan_next_integer changes the p pointer to forward the correct number of characters.
source share