I found a not very funny thing when programming: the strcmp function returns different values under the same conditions on different systems and even when run on the same system through valgrind ! In short, I created a program, which, depending on the value of strcmp , determines what command it was given to it via switch . Tobish if 0 is put , if 9 is get , etc. But, if I run the program through valgrind or on Windows, then the function returns only the characters -1 0 1 . Why is that?
Closed due to the fact that the essence of the issue is incomprehensible to the participants of PinkTux , andreymal , Cyril Malyshev , Edward , insolor Jan 19 '18 at 19:05 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
1 answer
Open the standard:
7.24.4.2 The strcmp function
Synopsis
#include <string.h> int strcmp(const char *s1, const char *s2);Description
strcmp.Returns
It has been
strcmpthan the srcmp.ISO / IEC 9899: 201x Committee Draft - April 12, 2011 N1570
Approximate translation:
7.24.4.2 The strcmp function
Synopsis
#include <string.h> int strcmp(const char *s1, const char *s2);Description
The
strcmpfunction compares the string pointed to bys1with the string pointed to bys2.Return value
The
strcmpfunction returns an integer greater than, equal to, or less than zero if the strings1greater than, equal to, or less than the strings2respectively.
The standard does not say what specific number to return. The main thing is that it should be greater than, less than or equal to zero (in appropriate cases). Those. the developer can implement it in any way you like. If s1 > s2 can be returned 1 , it is possible 2 , and it is possible 255 and such an implementation will conform to the standard.
This is how strcmp implemented in FreeBSD:
int strcmp(const char *s1, const char *s2) { while (*s1 == *s2++) if (*s1++ == '\0') return (0); return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1)); } So in Apple:
int strcmp(const char *s1, const char *s2) { for ( ; *s1 == *s2; s1++, s2++) if (*s1 == '\0') return 0; return ((*(unsigned char *)s1 < *(unsigned char *)s2) ? -1 : +1); } UPD
Until I began to reach the "true" meaning of the question.
The difference between
pandg== 9 - on Linuxstrcmpshows the difference between these letters, and on Windows it simply shows which of these letters is greater. Is it clear? - Andrej Levkovitch
As you can see, the FreeBSD code returns the difference between characters, and the Apple code (like Microsoft, apparently) ± 1.
- onePlease note that the standard (as is often the case) does not fully describe what it means less-more.
char *passed to the function, but in implementations the result of comparing 2unsigned charis returned - avp - one@avp, in fact, this moment is also specified in the standard. Clause 7.24.1 / 3 regarding all the functions from the header file
<string.h>says the following: "For all functions in this subclause, it is and has a different value) ". - wololo
strcmp()returns one of the three: a positive value, a negative value, zero ... Code in the studio, or the question is not clear ... - Fat-Zervalgrind? - Andrej Levkovitchstrcmptheswitchdetermines which command it was given to. Tobish if0isput, if9isgetand so on ..." - this is some kind of untranslatable fabulous pun. What is another9??? I need a code. - AnT