strsep(3)

โœจ strsep(3) โœจ

okay bestie, we're in the Library Functions Manual today and the star of the show is strsep, the string separate queen ๐Ÿ‘‘ she lives in the Standard C library (libc, -lc) and honestly? she's that girl. let's get into it ๐Ÿ’…


๐ŸŽ€ NAME ๐ŸŽ€

strsep โ€” string separate, periodt.

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc). yeah she's in libc, she's not some obscure little side quest ๐Ÿซถ

๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <string.h>

char *strsep(char **restrict stringp, const char *restrict delim);

Feature Test Macro Requirements for glibc (go peep feature_test_macros(7) for the full tea โ˜•):

  • strsep():
    • Since glibc 2.19:
      • _DEFAULT_SOURCE
    • glibc 2.19 and earlier:
      • _BSD_SOURCE

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

okay so here's the vibe. if *stringp is NULL, strsep() just returns NULL and does literally nothing else. no drama, no side quests, she said "not today" ๐Ÿ’€

otherwise? she goes hunting for the first token in the string *stringp that's delimited by one of the bytes in the string delim. and the way she does it is iconic: this token gets terminated by overwriting the delimiter with a null byte ('\0'), and then *stringp gets updated to point past the token. so yeah, she's out here editing in place, main character energy only โœจ

and if no delimiter was found at all? no worries babes โ€” the token is just taken to be the entire string *stringp, and *stringp is made NULL. slay.

๐Ÿ’– RETURN VALUE ๐Ÿ’–

strsep() returns a pointer to the token. which is, like, literally just the original value of *stringp. she gives you back what you gave her, but transformed ๐Ÿฆ‹

โญ ATTRIBUTES โญ

for an explanation of the terms used in this section, go check attributes(7) bestie.

  • Interface: strsep()
    • Attribute: Thread safety
    • Value: MT-Safe

translation: she's MT-Safe, she can hang in a crowd, no cap ๐Ÿซถ

๐ŸŒˆ STANDARDS ๐ŸŒˆ

BSD.

๐Ÿ’œ HISTORY ๐Ÿ’œ

4.4BSD.

and not me telling you that strsep() was introduced as a replacement for strtok(3) ๐ŸŒˆ because strtok(3) literally cannot handle empty fields. that's the whole origin story. strtok said "no empty fields" and strsep said "bet" ๐Ÿ’…

โš ๏ธ CAVEATS โš ๏ธ

be cautious when using this function, fr fr. if you do use it, note that: ๐Ÿ‘‡
  • the identity of the delimiting character is lost. like, gone. she overwrites it with a null byte, so that lil delimiter does not survive the encounter ๐Ÿ˜ญ

๐Ÿ”ฅ EXAMPLES ๐Ÿ”ฅ

the program below is a port of the one found in strtok(3) โ€” except this one doesn't discard multiple delimiters or empty tokens. so you get to see every single one, including the empty little guys ๐Ÿ™ˆ

$ ./a.out 'a/bbb///cc;xxx:yyy:' ':;' '/'
1: a/bbb///cc
        --> a
        --> bbb
        -->
        -->
        --> cc
2: xxx
        --> xxx
3: yyy
        --> yyy
4:
        -->

๐Ÿงš Program source ๐Ÿงš

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc, char *argv[])
{
    char *token, *subtoken;

    if (argc != 4) {
        fprintf(stderr, "Usage: %s string delim subdelim\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    for (unsigned int j = 1; (token = strsep(&argv[1], argv[2])); j++) {
        printf("%u: %s\n", j, token);

        while ((subtoken = strsep(&token, argv[3])))
            printf("\t --> %s\n", subtoken);
    }

    exit(EXIT_SUCCESS);
}

๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

string(3)


and that's the whole page babes! this iconic lil number is from Linux man-pages 6.20, dated 2026-08-10, and yes she signs her name strsep(3) at the bottom like the queen she is ๐Ÿ‘‘๐Ÿ’– go separate some strings, you got this ๐Ÿซถโœจ

๐Ÿ“ฆ 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