3 * gcc scanfprob.c -o scanfprob -Wall -W -Wextra -ansi -pedantic
15 * When scanf is attempting to convert numbers, any non-numeric characters
16 * it encounters terminate the conversion and are left in the input stream.
17 * Therefore, unexpected non-numeric input ``jams'' scanf again and again.
18 * E.g. Try to type a char and see what happens with the following code:
25 * See also: http://c-faq.com/stdio/scanfjam.html
28 /* First approach: check against the return value of scanf(3) */
36 /* Second approach: combine fgets(3) with sscanf(3) */
37 getchar(); /* Trim '\n' from previous */
40 fgets(buffer
, sizeof buffer
, stdin
);
41 rv
= sscanf(buffer
, "%d", &a
);
42 } while (rv
== 0 || a
!= -1);