1.\" Copyright (c) 1990, 1991, 1993, 1994 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" the American National Standards Committee X3, on Information 6.\" Processing Systems. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.Dd August 15, 2026 33.Dt BSEARCH 3 34.Os 35.Sh NAME 36.Nm bsearch , 37.Nm bsearch_b , 38.Nm bsearch_s 39.Nd binary search of a sorted table 40.Sh LIBRARY 41.Lb libc 42.Sh SYNOPSIS 43.In stdlib.h 44.Ft "QVoid *" 45.Fn bsearch "const void *key" "QVoid *base" "size_t nmemb" "size_t size" "int (*compar) (const void *, const void *)" 46.Ft "QVoid *" 47.Fn bsearch_b "const void *key" "QVoid *base" "size_t nmemb" "size_t size" "int (^compar) (const void *, const void *)" 48.Fd #define __STDC_WANT_LIB_EXT1__ 1 49.Ft "QVoid *" 50.Fn bsearch_s "const void *key" "QVoid *base" "rsize_t nmemb" "rsize_t size" "int (*compar) (const void *, const void *, void *)" "void *context" 51.Sh DESCRIPTION 52The 53.Fn bsearch 54function searches an array of 55.Fa nmemb 56objects, the initial member of which is 57pointed to by 58.Fa base , 59for a member that matches the object pointed to by 60.Fa key . 61The size of each member of the array is specified by 62.Fa size . 63.Pp 64The contents of the array should be in ascending sorted order according 65to the comparison function referenced by 66.Fa compar . 67The 68.Fa compar 69routine 70is expected to have 71two arguments which point to the 72.Fa key 73object and to an array member, in that order, and should return an integer 74less than, equal to, or greater than zero if the 75.Fa key 76object is found, respectively, to be less than, to match, or be 77greater than the array member. 78See the 79.Fa int_compare 80sample function in 81.Xr qsort 3 82for a comparison function that is also compatible with 83.Fn bsearch . 84.Pp 85The 86.Fn bsearch_b 87function behaves identically to 88.Fn bsearch , 89except the callback 90.Fa compar 91takes a block pointer instead of a function pointer. 92.Pp 93The 94.Fn bsearch_s 95function behaves identically to 96.Fn bsearch , 97except the callback 98.Fa compar 99is called with a third argument, 100.Fa context , 101which is passed through from the caller. 102Runtime-constraint violation occurs if: 103.Bl -bullet 104.It 105.Fa nmemb 106or 107.Fa size 108is greater than 109.Dv RSIZE_MAX 110.It 111.Fa nmemb 112is not zero and any of 113.Fa key , 114.Fa base , 115or 116.Fa compar 117is a null pointer 118.El 119.Pp 120On a runtime-constraint violation, the runtime-constraint handler is 121invoked, 122.Fn bsearch_s 123does not search the array, and a null pointer is returned. 124Note that the handler is called before 125.Fn bsearch_s 126returns, and the handler function might not return. 127If 128.Fa nmemb 129is zero, the comparison function is not called, no match is found, 130and 131.Fa key , 132.Fa base , 133and 134.Fa compar 135may be null pointers. 136.Sh RETURN VALUES 137The 138.Fn bsearch , 139.Fn bsearch_b , 140and 141.Fn bsearch_s 142functions return a pointer to a matching member of the array, or a null 143pointer if no match is found. 144.Fn bsearch_s 145also returns a null pointer if there is a runtime-constraint violation. 146If two members compare as equal, which member is matched is unspecified. 147.Sh EXAMPLES 148A sample program that searches people by age in a sorted array: 149.Bd -literal 150#include <assert.h> 151#include <stdint.h> 152#include <stdio.h> 153#include <stdlib.h> 154#include <string.h> 155 156struct person { 157 const char *name; 158 int age; 159}; 160 161static int 162compare(const void *a, const void *b) 163{ 164 const int *age; 165 const struct person *person; 166 167 age = a; 168 person = b; 169 170 return (*age - person->age); 171} 172 173int 174main(void) 175{ 176 struct person *friend; 177 int age; 178 /* Sorted array */ 179 const struct person friends[] = { 180 { "paul", 22 }, 181 { "anne", 25 }, 182 { "fred", 25 }, 183 { "mary", 27 }, 184 { "mark", 35 }, 185 { "bill", 50 } 186 }; 187 const size_t len = sizeof(friends) / sizeof(friends[0]); 188 189 age = 22; 190 friend = bsearch(&age, friends, len, sizeof(friends[0]), compare); 191 assert(strcmp(friend->name, "paul") == 0); 192 printf("name: %s\enage: %d\en", friend->name, friend->age); 193 194 age = 25; 195 friend = bsearch(&age, friends, len, sizeof(friends[0]), compare); 196 197 /* 198 * For multiple elements with the same key, it is implementation 199 * defined which will be returned 200 */ 201 assert(strcmp(friend->name, "fred") == 0 || 202 strcmp(friend->name, "anne") == 0); 203 printf("name: %s\enage: %d\en", friend->name, friend->age); 204 205 age = 30; 206 friend = bsearch(&age, friends, len, sizeof(friends[0]), compare); 207 assert(friend == NULL); 208 printf("friend aged 30 not found\en"); 209} 210.Ed 211.Sh SEE ALSO 212.Xr db 3 , 213.Xr lsearch 3 , 214.Xr qsort 3 , 215.Xr set_constraint_handler_s 3 216.\" .Xr tsearch 3 217.Sh STANDARDS 218The 219.Fn bsearch 220function conforms to 221.St -isoC-2023 , 222where it is specified as a qualifier-preserving function. 223.Pp 224The 225.Fn bsearch_s 226function conforms to 227.St -isoC-2023 , 228section K.3.6.3.2. 229Like 230.Fn bsearch , 231it is specified as a qualifier-preserving function. 232.Sh HISTORY 233The 234.Fn bsearch , 235initially specified in the 236.St -svid1 , 237first appeared in 238.At V.2 239and later in 240.Bx 4.3 . 241It was first standardized in 242.St -isoC . 243.Pp 244The 245.Fn bsearch_b 246function first appeared in Mac OS X and was later added to 247.Fx 11.0 248as an extension. 249.Pp 250The 251.Fn bsearch_s 252function first appeared in 253.Fx 16.0 . 254It was first standardized in 255.St -isoC-2011 . 256