The C bsearch() function searches a sorted array efficiently. It repeatedly compares the key with a middle element and discards half of the remaining range. If duplicates exist, which matching element is returned is unspecified.
This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. Success returns a pointer to one matching element. Failure to find a match returns NULL; check before dereferencing. The examples below move from the direct call to an edge condition and a practical use.
Comparator returning negative, zero, or positive for key versus element.
Return Value
Return Value
Description
Pointer to element
A matching array element was found.
NULL
No matching element exists.
C Examples
Example 1 — Find a present value
This complete program demonstrates a distinct part of the bsearch() contract and checks the result or precondition that matters for the scenario.
C
#include <stdio.h>
#include <stdlib.h>
static int compare_int(const void *a, const void *b) {
int left = *(const int *)a;
int right = *(const int *)b;
return (left > right) - (left < right);
}
int main(void){int a[]={1,3,5,7,9},key=7;int *p=bsearch(&key,a,5,sizeof a[0],compare_int);if(p) printf("found %d\n",*p);}
Output
found 7
How It Works
Step 1. Start with an array already sorted by compare_int.
Step 2. Pass the address of the search key.
Step 3. bsearch() narrows the candidate range using the comparator.
Step 4. A non-NULL result points at the matching element.
Example 2 — Handle a missing value
This complete program demonstrates a distinct part of the bsearch() contract and checks the result or precondition that matters for the scenario.
C
#include <stdio.h>
#include <stdlib.h>
static int compare_int(const void *a, const void *b) {
int left = *(const int *)a;
int right = *(const int *)b;
return (left > right) - (left < right);
}
int main(void){int a[]={2,4,6},key=5;int *p=bsearch(&key,a,3,sizeof a[0],compare_int);puts(p?"found":"not found");}
Output
not found
How It Works
Step 1. Keep the array in ascending order.
Step 2. Search for a value between two entries.
Step 3. No comparator equality is found.
Step 4. The returned NULL pointer selects the not-found branch.
Example 3 — Sort before searching
This complete program demonstrates a distinct part of the bsearch() contract and checks the result or precondition that matters for the scenario.
C
#include <stdio.h>
#include <stdlib.h>
static int compare_int(const void *a, const void *b) {
int left = *(const int *)a;
int right = *(const int *)b;
return (left > right) - (left < right);
}
int main(void){int a[]={8,1,4,2},key=4;qsort(a,4,sizeof a[0],compare_int);int *p=bsearch(&key,a,4,sizeof a[0],compare_int);printf("%s\n",p?"found":"missing");}
Output
found
How It Works
Step 1. Begin with data that is not ordered.
Step 2. Sort using the exact comparator intended for search.
Step 3. Call bsearch() only after ordering is established.
Step 4. The matching pointer confirms the lookup.
Frequently Asked Questions
bsearch() searches a sorted array efficiently. It repeatedly compares the key with a middle element and discards half of the remaining range. If duplicates exist, which matching element is returned is unspecified.
Include <stdlib.h> before calling bsearch().
Success returns a pointer to one matching element. Failure to find a match returns NULL; check before dereferencing.
The array must already be sorted according to the same ordering implemented by the comparator. Avoid subtraction comparators because signed overflow can break ordering.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.