4 ms·
> 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 r
by nathell 3y ago
> 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’.
- Decabytes 3y agoI've added a footnote to my incorrect explanation and credited you. I'm still a C noob so thank you for pointing this out!
- andoma 3y agoReminds me of the incorrect cast of memcmp() return value that resulted in this bug: https://bugs.mysql.com/bug.php?id=64884 https://bugs.mysql.com/bug.php?id=64884
- cassepipe 3y agoI don't have MacOs to prove it but I believe `strcmp` on MacOs returns either 0, 1 or -1
- skywal_l 3y agohttps://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/strcmp.3.html https://developer.apple.com/library/archive/documentation/Sy...
- cpeterso 3y agostrcmp'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;
- saagarjha 3y agoIt currently returns a character difference. Don’t rely on it, though!