πΎ Archived View for gmi.noulin.net βΊ man βΊ man3 βΊ strncmp.3.gmi captured on 2023-09-08 at 17:09:14. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
STRCMP(3) Linux Programmer's Manual STRCMP(3) NAME strcmp, strncmp - compare two strings SYNOPSIS #include <string.h> int strcmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t n); DESCRIPTION The strcmp() function compares the two strings s1 and s2. The locale is not taken into account (for a locale-aware comparison, see strcoll(3)). The comparison is done using unsigned characters. strcmp() returns an integer indicating the result of the comparison, as follows: β’ 0, if the s1 and s2 are equal; β’ a negative value if s1 is less than s2; β’ a positive value if s1 is greater than s2. The strncmp() function is similar, except it compares only the first (at most) n bytes of s1 and s2. RETURN VALUE The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2. ATTRIBUTES For an explanation of the terms used in this section, see attributes(7). ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββ¬ββββββββββ βInterface β Attribute β Value β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββββββββββββββββΌββββββββββ€ βstrcmp(), strncmp() β Thread safety β MT-Safe β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββ΄ββββββββββ CONFORMING TO POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD. NOTES POSIX.1 specifies only that: The sign of a nonzero return value shall be determined by the sign of the difference between the values of the first pair of bytes (both interpreted as type unsigned char) that differ in the strings being compared. In glibc, as in most other implementations, the return value is the arithmetic result of subtracting the last compared byte in s2 from the last compared byte in s1. (If the two characters are equal, this difference is 0.) EXAMPLES The program below can be used to demonstrate the operation of strcmp() (when given two arguments) and strncmp() (when given three arguments). First, some examβ ples using strcmp(): $ ./string_comp ABC ABC <str1> and <str2> are equal $ ./string_comp ABC AB # 'C' is ASCII 67; 'C' - '\0' = 67 <str1> is greater than <str2> (67) $ ./string_comp ABA ABZ # 'A' is ASCII 65; 'Z' is ASCII 90 <str1> is less than <str2> (-25) $ ./string_comp ABJ ABC <str1> is greater than <str2> (7) $ ./string_comp