News for hackers. Stuff that matters.
Re:C Strings and my slow descent to madness by nathell on Thursday April 06, 2023 @09:55AM (#35468341)
> Our last function is strcmp. It looks at two strings and determines whether they are equal to each other or not. If they are it returns 0. If they aren’t it returns 1.

No it doesn’t.

    RETURN VALUES
         The strcmp() and strncmp() functions return an integer greater than, equal
         to, or less than 0, according as the string s1 is greater than, equal to,
         or less than the string s2.  The comparison is done using unsigned
         characters, so that ‘\200’ is greater than ‘\0’.
Re:C Strings and my slow descent to madness by Decabytes on Thursday April 06, 2023 @10:32AM (#35468788)
I've added a footnote to my incorrect explanation and credited you. I'm still a C noob so thank you for pointing this out!
Re:C Strings and my slow descent to madness by andoma on Thursday April 06, 2023 @11:06AM (#35469312)
Reminds me of the incorrect cast of memcmp() return value that resulted in this bug: https://bugs.mysql.com/bug.php?id=64884
Re:C Strings and my slow descent to madness by cassepipe on Thursday April 06, 2023 @03:41PM (#35473354)
I don't have MacOs to prove it but I believe `strcmp` on MacOs returns either 0, 1 or -1
Re:C Strings and my slow descent to madness by skywal_l on Thursday April 06, 2023 @03:55PM (#35473517)
Re:C Strings and my slow descent to madness by cpeterso on Thursday April 06, 2023 @05:42PM (#35474920)
strcmp's return value is loosely defined because some implementations return the difference between the characters in the string to avoid some conditional checks or jumps. Something like:

  int d = a[i] - b[i];
  if (d == 0) return d;
Re:C Strings and my slow descent to madness by saagarjha on Thursday April 06, 2023 @09:30PM (#35476945)
It currently returns a character difference. Don’t rely on it, though!