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 categoryLC_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 bysort(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
- Copyright 2001, Andries E. Brouwer <aeb@cwi.nl>
- Copyright 2016, Michael Kerrisk <mtk.manpages@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms