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 .

  • one
    strcmp() returns one of the three: a positive value, a negative value, zero ... Code in the studio, or the question is not clear ... - Fat-Zer
  • five
    Because according to the standard, it returns either zero, or more than zero, or less than zero. And what exactly is more / less than zero - whoever wants, he does. In all the textbooks on C it is written not to attach to specific return values - andreymal
  • Well, okay on different systems - but why does it work differently when launched via valgrind ? - Andrej Levkovitch
  • 3
    No code - no question. - PinkTux
  • 2
    @Andrej Levkovitch: The second part of the question without code is completely incomprehensible. "... depending on the value of strcmp the switch determines which command it was given to. Tobish if 0 is put , if 9 is get and so on ..." - this is some kind of untranslatable fabulous pun. What is another 9 ??? I need a code. - AnT

1 answer 1

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 strcmp than 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 strcmp function compares the string pointed to by s1 with the string pointed to by s2 .

Return value

The strcmp function returns an integer greater than, equal to, or less than zero if the string s1 greater than, equal to, or less than the string s2 respectively.

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 p and g == 9 - on Linux strcmp shows 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.

  • one
    Please 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 2 unsigned char is 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