This documentation is archived and is not being maintained.


Performs a binary search of a sorted array.

void *bsearch( 
   const void *key,
   const void *base,
   size_t num,
   size_t width,
   int ( __cdecl *compare ) ( const void *, const void *) 


Object to search for.
Pointer to base of search data.
Number of elements.
Width of elements.
Callback function that compares two elements. The first is a pointer to the key for the search and the second is a pointer to the array element to be compared with the key.

Return Value

bsearch returns a pointer to an occurrence of key in the array pointed to by base. If key is not found, the function returns NULL. If the array is not in ascending sort order or contains duplicate records with identical keys, the result is unpredictable.


The bsearch function performs a binary search of a sorted array of num elements, each of width bytes in size. The base value is a pointer to the base of the array to be searched, and key is the value being sought. The compare parameter is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. bsearch calls the compare routine one or more times during the search, passing pointers to two array elements on each call. The compare routine compares the elements, then returns one of the following values:

Value returned by compare routine Description
< 0 elem1 less than elem2
0 elem1 equal to elem2
> 0 elem1 greater than elem2


Routine Required header Compatibility
bsearch <stdlib.h> and <search.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


This program reads the command-line parameters, sorting them with qsort, and then uses bsearch to find the word "cat".

// crt_bsearch.c
#include <search.h>
#include <string.h>
#include <stdio.h>

// Declare a function for compare
int compare( char **arg1, char **arg2 ); 

int main( int argc, char **argv )
   char **result;
   char *key = "cat";
   int i;

   /* Sort using Quicksort algorithm: */
   qsort( (void *)argv, (size_t)argc, sizeof( char * ), (int (*)(const 
   void*, const void*))compare );

   for( i = 0; i < argc; ++i )    /* Output sorted list */
      printf( "%s ", argv[i] );

   /* Find the word "cat" using a binary search algorithm: */
   result = (char **)bsearch( (char *) &key, (char *)argv, argc,
                              sizeof( char * ), (int (*)(const void*, const void*))compare );
   if( result )
      printf( "\n%s found at %Fp\n", *result, result );
      printf( "\nCat not found!\n" );

int compare( char **arg1, char **arg2 )
   /* Compare all of both strings: */
   return _strcmpi( *arg1, *arg2 );


dog pig horse cat human rat cow goat

Sample Output

bsearch cat cow dog goat horse human pig rat
cat found at 002F0F04

See Also

Searching and Sorting Routines | _lfind | _lsearch | qsort | Run-Time Routines and .NET Framework Equivalents