strspn(3)

๐Ÿ’– strspn โ€” bestie, it's string [complementary] span ๐Ÿ’–

okay so you're here for strspn(3) and strcspn(3), and honestly? iconic behavior. these are library functions from the standard C library, aka Standard C library (libc, -lc) โ€” the one you already have, no extra installs, no drama. ๐ŸŽ€


โœจ SYNOPSIS โœจ

you gotta include the header first, bestie, no skipping:

#include <string.h>

size_t strspn(const char *s, const char *accept);
size_t strcspn(const char *s, const char *reject);

two functions, two signatures, both returning size_t. slay. ๐Ÿ’…


๐ŸŒธ DESCRIPTION ๐ŸŒธ

strspn()

so strspn() looks at s and counts the length (in bytes) of the initial segment of s โ€” like, the very beginning, no gaps, from byte zero โ€” and that segment has to be made up entirely of bytes that also live in accept. the moment a byte shows up that's not in accept, it stops counting. that's it. that's the whole vibe check. ๐Ÿฆ‹

strcspn()

strcspn() is the complementary twin (see the NAME up there โ€” "complementary span", it's giving duality โœจ). it counts the length of the initial segment of s that consists entirely of bytes not in reject. so it walks forward until it hits a byte that is in reject, and everything before that is your answer.

and fr fr, it's equivalent to this exact expression:

(strpbrk(s, reject) ?: strnul(s)) - s

lowkey elegant. highkey underrated. ๐Ÿ”ฅ


๐Ÿซถ RETURN VALUE ๐Ÿซถ

  • strspn() returns the number of bytes in the initial segment of s which consist only of bytes from accept.
  • strcspn() returns the number of bytes in the initial segment of s which are not in the string reject.

both give you a size_t. no cap. ๐Ÿ’œ


๐Ÿ‘‘ ATTRIBUTES ๐Ÿ‘‘

if you wanna know what these words even mean, bestie, check out attributes(7) โ€” it explains all the terms used in this section.

  • Interface: strspn(), strcspn() โ€” Attribute: Thread safety โ€” Value: MT-Safe

so yeah, both of them are MT-Safe. you can call them from multiple threads and it's fine. main character energy, thread-safe edition. ๐Ÿงš


โญ STANDARDS โญ

  • C11
  • POSIX.1-2008

certified, standardized, periodt. ๐Ÿ“œ


๐ŸŒˆ HISTORY ๐ŸŒˆ

where did these come from? glad you asked:

  • POSIX.1-2001
  • C89
  • SVr4
  • 4.3BSD

ancient lineage. they've been serving since basically forever. ๐Ÿ’€โœจ


๐Ÿ”ฅ SEE ALSO ๐Ÿ”ฅ

go read these next, they're all in the family:


anyway bestie that's the whole page. this was pulled from Linux man-pages 6.20, dated 2026-08-10, for strspn(3) โ€” so like, blame the version if anything's weird, not me. mwah, go count some spans. ๐Ÿ’–๐ŸŽ€๐ŸŒธ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms