Before we even look at the interface and algorithm, there's a few issues to address:
<string.h>
and<stddef.h>
aren't required, as far as I can see (note that<stdlib.h>
must providesize_t
, if that's why the latter was included).int main()
is a non-prototype definition; preferint main(void)
instead.There's no check that
scanf()
successfully converted a value. This lack of checking is a serious bug. Ifscanf()
doesn't return 1 here, we need to take corrective action (consume input and prompt again, or just write a message tostderr
and returnEXIT_FAILURE
).We attempt to print the results using
%d
withsize_t
values - we should be using%zd%zu
instead.
As for the get_num_of_ints()
function, there are many narrowing and sign-converting conversions hidden there, due to use of int
variables which really ought to be size_t
(don't just blindly convert, because subtraction will overflow when r
is zero).
To be honest, I don't understand why you're implementing your own binary search from scratch instead of simply using the Standard Library bsearch()
for this.
Oh, and the function name is highly misleading, as the return value is not the number of matches; it's the index of first match.
I think all my other observations are mentioned in older answers; there's no point repeating those.