totn C Functions

C Language: bsearch function
(Binary Search)

In the C Programming Language, the bsearch function searches a sorted array stored at address base for the value pointed to by key.

Syntax

The syntax for the bsearch function in the C Language is:

void *bsearch(const void *key, const void *base, size_t num_members,
              size_t size, int (*compare_function)
              (const void *, const void *));

Parameters or Arguments

key
The value to search for.
base
The address in a sorted array to begin the search.
num_members
The number of elements.
size
The size of the elements in bytes.
compare_function
A pointer to a comparison function.

Returns

The bsearch function returns a negative, zero, or positive integer (based on whether key is less than, equal to, or greater than the element in the array). If key is not found, the bsearch function will return a null pointer.

Required Header

In the C Language, the required header for the bsearch function is:

#include <stdlib.h>

Applies To

In the C Language, the bsearch function can be used in the following versions:

  • ANSI/ISO 9899-1990

Similar Functions

Other C functions that are similar to the bsearch function: