C Programming/string.h/strcmp

In POSIX and in the programming language C,  is a function in the C standard library (declared in  ) that compares two C strings.

The prototype according ISO/IEC 9899:1999, 7.21.4.2

returns 0 when the strings are equal, a negative integer when  is less than , or a positive integer if   is greater than  , according to the lexicographical order.

A variant of  exists called   that only compares the strings up to a certain offset.

Another variant,, conforming to POSIX.1-2001, works like  , but is case-insensitive. Some systems instead provide this functionality with functions named  or. To compare a subset of both strings with case-insensitivity, various systems may provide,   or.

Example
The above code is a working sample that prints whether the first argument is less than, equal to or greater than the second.

A possible implementation is (P.J. Plauger, The Standard C Library, 1992): However, most real-world implementations will have various optimization tricks to reduce the execution time of the function. One will notice, that strcmp not only returns -1, 0 and +1, but also other negative or positive values, resulting from optimizing away the branching introduced by the  operator: