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 ofswhich consist only of bytes fromaccept.strcspn()returns the number of bytes in the initial segment ofswhich are not in the stringreject.
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
- Copyright 1993, David Metcalfe <david@prism.demon.co.uk>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms