bsearch function(Binary Search) in C Language – example and detail

In the C Programming Language, the bsearch characteristic searches a sorted array saved at tackle base for the fee 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 fee to search for. base The address in a sorted array to begin the search. num_members The variety of elements. size The size of the factors in bytes. compare_function A pointer to a comparison function.

Returns

The bsearch characteristic returns a negative, zero, or high quality integer (based on whether or not key is less than, equal to, or higher than the factor in the array). If key is not found, the bsearch characteristic will return a null pointer.

Required Header

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

#include <stdlib.h>

Applies To

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

ANSI/ISO 9899-1990

Similar Functions

Other C functions that are similar to the bsearch function:

qsort function <stdlib.h>