Search code examples
clanguage-lawyerstandard-librarymemcmp

What, exactly, is memcmp supposed to return?


I would like to know what the function memcmp must return.

I've been searching on the Internet, and usually, memcmp definitions state something like the following:

The memcmp() function returns an integer greater than, equal to, or less than zero, accordingly as the object pointed to by s1 is greater than, equal to, or less than the object pointed to by s2.

It is never explicitly said what, exactly, is returned: is it the difference between two byte values, or is it -1, 0 or 1? I am confused:

  • When testing the function memcmp in a little program, it returns -1, 0 or 1, even when the difference between the two bytes evaluated is higher than 1 or lower than -1.
  • When looking at functions named memcmp on the Internet, they almost all return the difference between 2 bytes, as an int, instead of returning either -1, 0 or 1.

Since I can't get a precise enough definition of the function memcmp, I ask this question here: what, exactly, is the function memcmp supposed to return? Is there an "official" source code somewhere? (I have seen a lot of source codes for memcmp but none gave me an answer: I then suppose that they are not the function that is written in the library string.h, at least not on my computer...)


Solution

  • The particular values returned by memcmp() are not specified by the Standard. The C11 Draft Standard does say, in §7.24.4 1:

    The sign of a nonzero value returned by the comparison functions memcmp, strcmp, and strncmp is determined by the sign of the difference between the values of the first pair of characters (both interpreted as unsigned char) that differ in the objects being compared.

    So only the sign of nonzero return values from the comparison functions should be taken as meaningful. The latitude given here allows each implementation to interpret these requirements as it sees fit.

    Also, note that there is no "official source code"; the Standard is the document that C implementations must adhere to. Even reading the source code for the implementation that you are using to find the underlying method used to generate the memcmp() return values, use of these values in code is at best not portable, and is vulnerable to future changes in that implementation.