Format
#include <stdlib.h> /* OS/2 and WIN also in <search.h> */
void *bsearch(const void *key, const void *base,
size_t num, size_t size,
int (*compare)(const void *key, const void *element));
Language Level: ANSI, POSIX, XPG, Extension
bsearch performs a binary search of an array of num
elements, each of size bytes. The array must be sorted
in ascending order by the function pointed to by compare.
The base is a pointer to the base of the array to
search, and key is the value being sought.
The compare argument is a pointer to a function you must supply that takes a pointer to the key argument and to an array element, in that order. bsearch calls this function one or more times during the search. The function must compare the key and the element and return one of the following values:
| Value | Meaning |
| Less than 0 | key less than element |
| 0 | key identical to element |
| Greater than 0 | key greater than element |
Return Value
bsearch returns a pointer to key in the array
to which base points. If two keys are equal, the
element that key will point to is unspecified. If
bsearch cannot find the key, it returns NULL.
![]()
lfind - lsearch -- Find Key in Array
qsort -- Sort Array
<stdlib.h>