Skip to main content
Use unsigned printf format for size_t
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

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 provide size_t, if that's why the latter was included).

  • int main() is a non-prototype definition; prefer int main(void) instead.

  • There's no check that scanf() successfully converted a value. This lack of checking is a serious bug. If scanf() doesn't return 1 here, we need to take corrective action (consume input and prompt again, or just write a message to stderr and return EXIT_FAILURE).

  • We attempt to print the results using %d with size_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.

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 provide size_t, if that's why the latter was included).

  • int main() is a non-prototype definition; prefer int main(void) instead.

  • There's no check that scanf() successfully converted a value. This lack of checking is a serious bug. If scanf() doesn't return 1 here, we need to take corrective action (consume input and prompt again, or just write a message to stderr and return EXIT_FAILURE).

  • We attempt to print the results using %d with size_t values - we should be using %zd 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.

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 provide size_t, if that's why the latter was included).

  • int main() is a non-prototype definition; prefer int main(void) instead.

  • There's no check that scanf() successfully converted a value. This lack of checking is a serious bug. If scanf() doesn't return 1 here, we need to take corrective action (consume input and prompt again, or just write a message to stderr and return EXIT_FAILURE).

  • We attempt to print the results using %d with size_t values - we should be using %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.

Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

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 provide size_t, if that's why the latter was included).

  • int main() is a non-prototype definition; prefer int main(void) instead.

  • There's no check that scanf() successfully converted a value. This lack of checking is a serious bug. If scanf() doesn't return 1 here, we need to take corrective action (consume input and prompt again, or just write a message to stderr and return EXIT_FAILURE).

  • We attempt to print the results using %d with size_t values - we should be using %zd 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.