Is there a Binary Search method in the C standard library?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Yes. The C standard library provides binary search through the bsearch function in stdlib.h. It is a low-level API designed for sorted arrays, so it works well when you already have contiguous memory and a comparison function, but it does not sort the data or provide insertion-point information for missing keys.
The bsearch Function
The standard signature is:
It returns:
- a pointer to a matching element if found
- '
NULLif no matching element exists'
The array must already be sorted according to the same ordering rule used by the comparator.
Basic Example with Integers
This works because the array is already sorted and the comparison function matches that ordering.
qsort and bsearch Often Go Together
In C, qsort and bsearch are natural companions. qsort sorts the array and bsearch searches it using the same comparator.
This is the typical standard-library workflow for sorted array lookup in C.
Arrays of Structs
bsearch becomes especially useful with arrays of records. You can search by a key field if the comparator knows how to compare the search key against an element.
This is a common pattern when you want a fast lookup in a sorted record array without introducing a separate hash table.
What bsearch Does Not Provide
bsearch is useful, but it is intentionally minimal. It does not:
- sort the array for you
- tell you where a missing element should be inserted
- guarantee which matching element you get if duplicates exist
- work on linked lists or other non-array data structures
If you need lower-bound or upper-bound semantics, or exact insertion positions, you usually write a custom binary search instead of relying on bsearch alone.
Comparator Quality Matters
The comparator must define the same ordering that the array was sorted with. If the array order and comparator disagree, the search result is unreliable.
Also avoid simplistic comparator code like:
That can overflow for large integers. The explicit comparison pattern is safer.
When bsearch Is the Right Tool
Use bsearch when:
- the data is in a sorted array
- the dataset is read-mostly
- you want a standard-library solution
- you do not need insertion-point information
If your data structure or query requirements do not match those assumptions, another approach may be better.
Common Pitfalls
The biggest mistake is calling bsearch on an unsorted array. Another is using a comparator that does not match the actual array ordering. Developers also often expect bsearch to report the insertion position for a missing key, which it does not. Finally, duplicate values can be surprising because bsearch does not guarantee which matching element it returns when more than one element compares equal.
Summary
- The C standard library provides binary search as
bsearchinstdlib.h. - '
bsearchworks on sorted arrays and returns a pointer to a matching element orNULL.' - Use the same comparison logic for both sorting and searching.
- '
qsortandbsearchare commonly paired.' - If you need insertion-point or duplicate-range behavior, write a custom search instead.
Related reading
- Is there a built-in Binary Search Tree in .NET 4.0?
- Is there a built in function for string natural sort?
- Is there a checksum algorithm that also supports subtracting data from it?
- Is there a diff-like algorithm that handles moving block of lines?
- Is there a Java equivalent or methodology for the typedef keyword in C++?
- Is there a nice way to assign stdminmaxa, b to stdtiea, b?
- Is there a difference between dfs and topological sort? Can topological ordering be achieved without using dfs?
- Is there a fast algorithm to determine the godel number of a term of a context free language?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.