0

What is a merit for the signed int return value of memcmp compared to say bool?

I don't think I've ever used the return value of memcmp other than to compare it against zero.

foreverska
  • 585
  • 3
  • 20
  • 1
    Did you read the manual ([`memcmp`](https://en.cppreference.com/w/c/string/byte/memcmp))? What's unclear? – Ted Lyngmo Nov 13 '22 at 17:40
  • There _are_ cases where you want to know if the first arg is less than, equal to, or greater than the second arg. – Craig Estey Nov 13 '22 at 17:51
  • @TedLyngmo: That is not “the” manual; it is “a” manual, and it is not standard documentation for `memcmp`. The C standard is silent about why `memcmp` returns a signed value rather than a Boolean, and so is the [POSIX standard](https://pubs.opengroup.org/onlinepubs/9699919799/). You should not be chastising people for not having read your choice of unofficial documentation. – Eric Postpischil Nov 13 '22 at 18:56
  • @EricPostpischil True, I should have said "a manual". I don't find the wording in the C standard that different from the online manual I referred to though. I wasn't chastising OP. I wanted to know if he/she had read about the function and if so, what part that was unclear. – Ted Lyngmo Nov 13 '22 at 21:05

1 Answers1

2

What is a merit for the signed int return value of memcmp

Useful for sorting objects based on their binary representation.

Note: memcmp() returns a +,0,- based on the first mismatched byte as if bytes are unsigned char, not char.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256