strverscmp(3)

✨ strverscmp β€” strings version compare ✨

okay bestie, so this page is from the Library Functions Manual chapter of the man pages, like the strverscmp(3) situation πŸ’–


πŸŽ€ NAME πŸŽ€

strverscmp β€” strings version compare

πŸ“š LIBRARY πŸ“š

Standard C library (libc, -lc) 🫢

🌸 SYNOPSIS 🌸

#define _GNU_SOURCE         /* See feature_test_macros(7) */
#include <string.h>

int strverscmp(const char *s1, const char *s2);

πŸ’… DESCRIPTION πŸ’…

so picture this: you've got a dataset like jan1, jan2, ..., jan9, jan10, ... and you sort it lexicographically and you get the disaster order jan1, jan10, ..., jan2, ..., jan9 😭 like nobody asked for that. strverscmp()'s whole job is to compare two strings and give you the former order (the cute one), while strcmp(3) only does the lexicographic thing. iconic behavior, honestly.

lowkey important: this function does NOT use the locale category LC_COLLATE, so it's meant mostly for situations where the strings are expected to be in ASCII. and heads up β€” this is different from the ordering produced by sort(1) -V. don't get it twisted πŸ’€

so what does it actually do? here's the tea:

  • if both strings are equal, return 0 🎯
  • otherwise, find the position between two bytes with the property that before it both strings are equal, while directly after it there's a difference
  • find the largest consecutive digit strings containing (or starting at, or ending at) this position
  • if one or both of these is empty, then return what strcmp(3) would have returned (numerical ordering of byte values)
  • otherwise, compare both digit strings numerically, where digit strings with one or more leading zeros are interpreted as if they have a decimal point in front (so that in particular digit strings with more leading zeros come before digit strings with fewer leading zeros)

and the resulting ordering for that last case is literally: 000, 00, 01, 010, 09, 0, 1, 9, 10. slay 🌈

πŸ”₯ RETURN VALUE πŸ”₯

strverscmp() returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be earlier than, equal to, or later than s2. periodt πŸ’–

πŸ¦‹ ATTRIBUTES πŸ¦‹

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

  • strverscmp() β€” Thread safety β€” MT-Safe βœ… (she's safe bestie, we love a multithread-friendly queen πŸ‘‘)

⭐ STANDARDS ⭐

GNU. that's it, that's the standard 🫢

🌸 EXAMPLES 🌸

the program below can be used to demonstrate the behavior of strverscmp(). it uses strverscmp() to compare the two strings given as its command-line arguments. an example of its use is the following:

$ ./a.out jan1 jan10;
jan1 < jan10

see?? jan1 < jan10 β€” the way it just works 🧚

πŸ’œ Program source πŸ’œ

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

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

    if (argc != 3) {
        fprintf(stderr, "Usage: %s <string1> <string2>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    res = strverscmp(argv[1], argv[2]);

    printf("%s %s %s\n", argv[1],
           (res < 0) ? "<" : (res == 0) ? "==" : ">", argv[2]);

    exit(EXIT_SUCCESS);
}

🌈 SEE ALSO 🌈


okay that's the whole page bestie, go forth and compare your version strings correctly πŸ’–βœ¨

mwah, this was Linux man-pages 6.20, dated 2026-08-10 β€” catch you on the next page πŸŽ€πŸ¦‹πŸ’…

πŸ“¦ 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