From the man page for scanf:
Upon successful completion, these functions shall return the number of successfully matched and assigned input items
and
An input item shall be defined as the longest sequence of input bytes (up to any specified maximum field width, which may be measured in characters or bytes dependent on the conversion specifier) which is an initial subsequence of a matching sequence. The first byte, if any, after the input item shall remain unread. If the length of the input item is 0, the execution of the conversion specification shall fail; this condition is a matching failure, unless end-of-file, an encoding error, or a read error prevented input from the stream, in which case it is an input failure.
In your case, the input specifier is "%d", which Matches an optionally signed decimal integer...
. So if the first character (after skipping any white space) is a numeric digit (or a +/- sign), this will convert the number (one or more digits), stopping when it sees any non-digit characters. The return value is the number of converted values, which will be 1 if it found a valid decimal number, or 0 if it didn't.
Beware that the result may not be what you expect, for example:
4-hydroxytoluene
would convert the number 4 and return 1, even though it doesn't appear to be the intent of the input. Similary 1337age
, for "leetage", would convert the number 1337. I often use a code snippet like the following to avoid such cases:
char dummy;
int conversions = scanf("%d%c", &number, &dummy);
if ((conversions != 1) && ((conversion != 2) || !isspace(dummy)))
printf("Entered value is not an integer.\n");