Weird return value in strcmp

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












While checking the return value of strcmp function, I found some strange behavior in gcc. Here's my code:



#include <stdio.h>
#include <string.h>

char str0 = "hello world!";
char str1 = "Hello world!";

int main()
printf("%dn", strcmp("hello world!", "Hello world!"));
printf("%dn", strcmp(str0, str1));



When I compile this with clang, both calls to strcmp return 32. However, when compiling with gcc, the first call returns 1, and the second call returns 32. I don't understand why the first and second calls to strcmp return different values when compiled using gcc.



Below is my test environment.



  • Ubuntu 18.04 64bit

  • gcc 7.3.0

  • clang 6.0.0









share|improve this question









New contributor




fips197 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 3




    What's "weird" about this?
    – melpomene
    1 hour ago






  • 3




    The standard only specifies the sign of the result; it does not specify the magnitude. If the strings are equal, the result is zero; if the first string sorts before the second, the result is negative; if the second string sorts after the second, the result is positive. In your example, both 1 and 32 are positive; the results are equivalent as far as the standard is concerned, and your code should be written so that it makes no difference to you, either.
    – Jonathan Leffler
    1 hour ago











  • Incidentally, the difference between 'h' and 'H' is 32. Coincidence?
    – Christian Gibbons
    1 hour ago






  • 1




    Thanks for the replies, but i alredy checked man pages and ISO document so I know the exact return value is not specified. I just want to know why there's a difference between literal string and array string in GCC.
    – fips197
    1 hour ago






  • 1




    Likely duplicate of Inconsistent strcmp() return value when passing strings as pointers or as literals ... Tl;DR; both are valid you are seeing the effects of optimization
    – Shafik Yaghmour
    22 mins ago















up vote
6
down vote

favorite












While checking the return value of strcmp function, I found some strange behavior in gcc. Here's my code:



#include <stdio.h>
#include <string.h>

char str0 = "hello world!";
char str1 = "Hello world!";

int main()
printf("%dn", strcmp("hello world!", "Hello world!"));
printf("%dn", strcmp(str0, str1));



When I compile this with clang, both calls to strcmp return 32. However, when compiling with gcc, the first call returns 1, and the second call returns 32. I don't understand why the first and second calls to strcmp return different values when compiled using gcc.



Below is my test environment.



  • Ubuntu 18.04 64bit

  • gcc 7.3.0

  • clang 6.0.0









share|improve this question









New contributor




fips197 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 3




    What's "weird" about this?
    – melpomene
    1 hour ago






  • 3




    The standard only specifies the sign of the result; it does not specify the magnitude. If the strings are equal, the result is zero; if the first string sorts before the second, the result is negative; if the second string sorts after the second, the result is positive. In your example, both 1 and 32 are positive; the results are equivalent as far as the standard is concerned, and your code should be written so that it makes no difference to you, either.
    – Jonathan Leffler
    1 hour ago











  • Incidentally, the difference between 'h' and 'H' is 32. Coincidence?
    – Christian Gibbons
    1 hour ago






  • 1




    Thanks for the replies, but i alredy checked man pages and ISO document so I know the exact return value is not specified. I just want to know why there's a difference between literal string and array string in GCC.
    – fips197
    1 hour ago






  • 1




    Likely duplicate of Inconsistent strcmp() return value when passing strings as pointers or as literals ... Tl;DR; both are valid you are seeing the effects of optimization
    – Shafik Yaghmour
    22 mins ago













up vote
6
down vote

favorite









up vote
6
down vote

favorite











While checking the return value of strcmp function, I found some strange behavior in gcc. Here's my code:



#include <stdio.h>
#include <string.h>

char str0 = "hello world!";
char str1 = "Hello world!";

int main()
printf("%dn", strcmp("hello world!", "Hello world!"));
printf("%dn", strcmp(str0, str1));



When I compile this with clang, both calls to strcmp return 32. However, when compiling with gcc, the first call returns 1, and the second call returns 32. I don't understand why the first and second calls to strcmp return different values when compiled using gcc.



Below is my test environment.



  • Ubuntu 18.04 64bit

  • gcc 7.3.0

  • clang 6.0.0









share|improve this question









New contributor




fips197 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











While checking the return value of strcmp function, I found some strange behavior in gcc. Here's my code:



#include <stdio.h>
#include <string.h>

char str0 = "hello world!";
char str1 = "Hello world!";

int main()
printf("%dn", strcmp("hello world!", "Hello world!"));
printf("%dn", strcmp(str0, str1));



When I compile this with clang, both calls to strcmp return 32. However, when compiling with gcc, the first call returns 1, and the second call returns 32. I don't understand why the first and second calls to strcmp return different values when compiled using gcc.



Below is my test environment.



  • Ubuntu 18.04 64bit

  • gcc 7.3.0

  • clang 6.0.0






c gcc clang






share|improve this question









New contributor




fips197 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




fips197 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 47 mins ago









Eric Schnipke

13213




13213






New contributor




fips197 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









fips197

364




364




New contributor




fips197 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





fips197 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






fips197 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 3




    What's "weird" about this?
    – melpomene
    1 hour ago






  • 3




    The standard only specifies the sign of the result; it does not specify the magnitude. If the strings are equal, the result is zero; if the first string sorts before the second, the result is negative; if the second string sorts after the second, the result is positive. In your example, both 1 and 32 are positive; the results are equivalent as far as the standard is concerned, and your code should be written so that it makes no difference to you, either.
    – Jonathan Leffler
    1 hour ago











  • Incidentally, the difference between 'h' and 'H' is 32. Coincidence?
    – Christian Gibbons
    1 hour ago






  • 1




    Thanks for the replies, but i alredy checked man pages and ISO document so I know the exact return value is not specified. I just want to know why there's a difference between literal string and array string in GCC.
    – fips197
    1 hour ago






  • 1




    Likely duplicate of Inconsistent strcmp() return value when passing strings as pointers or as literals ... Tl;DR; both are valid you are seeing the effects of optimization
    – Shafik Yaghmour
    22 mins ago













  • 3




    What's "weird" about this?
    – melpomene
    1 hour ago






  • 3




    The standard only specifies the sign of the result; it does not specify the magnitude. If the strings are equal, the result is zero; if the first string sorts before the second, the result is negative; if the second string sorts after the second, the result is positive. In your example, both 1 and 32 are positive; the results are equivalent as far as the standard is concerned, and your code should be written so that it makes no difference to you, either.
    – Jonathan Leffler
    1 hour ago











  • Incidentally, the difference between 'h' and 'H' is 32. Coincidence?
    – Christian Gibbons
    1 hour ago






  • 1




    Thanks for the replies, but i alredy checked man pages and ISO document so I know the exact return value is not specified. I just want to know why there's a difference between literal string and array string in GCC.
    – fips197
    1 hour ago






  • 1




    Likely duplicate of Inconsistent strcmp() return value when passing strings as pointers or as literals ... Tl;DR; both are valid you are seeing the effects of optimization
    – Shafik Yaghmour
    22 mins ago








3




3




What's "weird" about this?
– melpomene
1 hour ago




What's "weird" about this?
– melpomene
1 hour ago




3




3




The standard only specifies the sign of the result; it does not specify the magnitude. If the strings are equal, the result is zero; if the first string sorts before the second, the result is negative; if the second string sorts after the second, the result is positive. In your example, both 1 and 32 are positive; the results are equivalent as far as the standard is concerned, and your code should be written so that it makes no difference to you, either.
– Jonathan Leffler
1 hour ago





The standard only specifies the sign of the result; it does not specify the magnitude. If the strings are equal, the result is zero; if the first string sorts before the second, the result is negative; if the second string sorts after the second, the result is positive. In your example, both 1 and 32 are positive; the results are equivalent as far as the standard is concerned, and your code should be written so that it makes no difference to you, either.
– Jonathan Leffler
1 hour ago













Incidentally, the difference between 'h' and 'H' is 32. Coincidence?
– Christian Gibbons
1 hour ago




Incidentally, the difference between 'h' and 'H' is 32. Coincidence?
– Christian Gibbons
1 hour ago




1




1




Thanks for the replies, but i alredy checked man pages and ISO document so I know the exact return value is not specified. I just want to know why there's a difference between literal string and array string in GCC.
– fips197
1 hour ago




Thanks for the replies, but i alredy checked man pages and ISO document so I know the exact return value is not specified. I just want to know why there's a difference between literal string and array string in GCC.
– fips197
1 hour ago




1




1




Likely duplicate of Inconsistent strcmp() return value when passing strings as pointers or as literals ... Tl;DR; both are valid you are seeing the effects of optimization
– Shafik Yaghmour
22 mins ago





Likely duplicate of Inconsistent strcmp() return value when passing strings as pointers or as literals ... Tl;DR; both are valid you are seeing the effects of optimization
– Shafik Yaghmour
22 mins ago













4 Answers
4






active

oldest

votes

















up vote
6
down vote



accepted










It looks like you didn't enable optimizations (e.g. -O2).



From my tests it looks like gcc always recognizes strcmp with constant arguments and optimizes it, even with -O0 (no optimizations). Clang needs at least -O1 to do so.



That's where the difference comes from: The code produced by clang calls strcmp twice, but the code produced by gcc just does printf("%dn", 1) in the first case because it knows that 'h' > 'H' (ASCIIbetically, that is). It's just constant folding, really.



Live example: https://godbolt.org/z/8Hg-gI



As the other answers explain, any positive value will do to indicate that the first string is greater than the second, so the compiler optimizer simply chooses 1. The strcmp library function apparently uses a different value.






share|improve this answer
















  • 2




    Although it's interesting that Clang and GCC can be induced to compile the program either such that their respective results produce the same output or such that they don't, I don't like interpreting that as optimization or lack thereof being the reason for the output to differ. It would be better to generalize that to "implementation details", as optimization is only one reason why the results might differ, whether in this specific case or (even more so) in the general case.
    – John Bollinger
    1 hour ago










  • You can also use -fbo-builtin to observe some of these effects
    – Shafik Yaghmour
    20 mins ago

















up vote
6
down vote













The standard defines the result of strcmp to be negative, if lhs appears before rhs in lexical order, zero if they are equal, or a positive value if lhs appears lexically after rhs.



It's up to the implementation how to implement that and what exactly to return. You must not depend on a specific value in your programs, or they won't be portable. Simply check with comparisons (<, >, ==).



See https://en.cppreference.com/w/c/string/byte/strcmp



Background



One simple implementation might just calculate the difference of each character c1 - c2 and do that until the result is not zero, or one of the strings ends. The result will then be the numeric difference between the first character, in which the two strings differed.



For example, this GLibC implementation: https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=string/strcmp.c;hb=HEAD






share|improve this answer





























    up vote
    5
    down vote













    The strcmp function is only specified to return a value larger than zero, zero, or less than zero. There's nothing specified what those positive and negative values have to be.






    share|improve this answer



























      up vote
      3
      down vote













      The exact values returned by strcmp in the case of the strings not being equal are not specified. From the man page:




      #include <string.h>
      int strcmp(const char *s1, const char *s2);
      int strncmp(const char *s1, const char *s2, size_t n);


      The strcmp() and strncmp() functions return an integer less than,
      equal to, or greater than zero if s1 (or the first n bytes thereof) is
      found, respectively, to be less than, to match, or be greater than s2.




      Since str1 compares greater than str2, the value must be positive, which it is in both cases.



      As for the difference between the two compilers, it appears that clang is returning the difference between the ASCII values for the corresponding characters that mismatched, while gcc is opting for a simple -1, 0, or 1. Both are valid, so your code should only need to check if the value is 0, greater than 0, or less than 0.






      share|improve this answer
















      • 1




        The interesting thing is that gcc only gave 1 when passing in the string literals. I suspect it may have been an optimization knowing that the result would always be the same.
        – Christian Gibbons
        1 hour ago










      Your Answer





      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );






      fips197 is a new contributor. Be nice, and check out our Code of Conduct.









       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52334056%2fweird-return-value-in-strcmp%23new-answer', 'question_page');

      );

      Post as a guest






























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      6
      down vote



      accepted










      It looks like you didn't enable optimizations (e.g. -O2).



      From my tests it looks like gcc always recognizes strcmp with constant arguments and optimizes it, even with -O0 (no optimizations). Clang needs at least -O1 to do so.



      That's where the difference comes from: The code produced by clang calls strcmp twice, but the code produced by gcc just does printf("%dn", 1) in the first case because it knows that 'h' > 'H' (ASCIIbetically, that is). It's just constant folding, really.



      Live example: https://godbolt.org/z/8Hg-gI



      As the other answers explain, any positive value will do to indicate that the first string is greater than the second, so the compiler optimizer simply chooses 1. The strcmp library function apparently uses a different value.






      share|improve this answer
















      • 2




        Although it's interesting that Clang and GCC can be induced to compile the program either such that their respective results produce the same output or such that they don't, I don't like interpreting that as optimization or lack thereof being the reason for the output to differ. It would be better to generalize that to "implementation details", as optimization is only one reason why the results might differ, whether in this specific case or (even more so) in the general case.
        – John Bollinger
        1 hour ago










      • You can also use -fbo-builtin to observe some of these effects
        – Shafik Yaghmour
        20 mins ago














      up vote
      6
      down vote



      accepted










      It looks like you didn't enable optimizations (e.g. -O2).



      From my tests it looks like gcc always recognizes strcmp with constant arguments and optimizes it, even with -O0 (no optimizations). Clang needs at least -O1 to do so.



      That's where the difference comes from: The code produced by clang calls strcmp twice, but the code produced by gcc just does printf("%dn", 1) in the first case because it knows that 'h' > 'H' (ASCIIbetically, that is). It's just constant folding, really.



      Live example: https://godbolt.org/z/8Hg-gI



      As the other answers explain, any positive value will do to indicate that the first string is greater than the second, so the compiler optimizer simply chooses 1. The strcmp library function apparently uses a different value.






      share|improve this answer
















      • 2




        Although it's interesting that Clang and GCC can be induced to compile the program either such that their respective results produce the same output or such that they don't, I don't like interpreting that as optimization or lack thereof being the reason for the output to differ. It would be better to generalize that to "implementation details", as optimization is only one reason why the results might differ, whether in this specific case or (even more so) in the general case.
        – John Bollinger
        1 hour ago










      • You can also use -fbo-builtin to observe some of these effects
        – Shafik Yaghmour
        20 mins ago












      up vote
      6
      down vote



      accepted







      up vote
      6
      down vote



      accepted






      It looks like you didn't enable optimizations (e.g. -O2).



      From my tests it looks like gcc always recognizes strcmp with constant arguments and optimizes it, even with -O0 (no optimizations). Clang needs at least -O1 to do so.



      That's where the difference comes from: The code produced by clang calls strcmp twice, but the code produced by gcc just does printf("%dn", 1) in the first case because it knows that 'h' > 'H' (ASCIIbetically, that is). It's just constant folding, really.



      Live example: https://godbolt.org/z/8Hg-gI



      As the other answers explain, any positive value will do to indicate that the first string is greater than the second, so the compiler optimizer simply chooses 1. The strcmp library function apparently uses a different value.






      share|improve this answer












      It looks like you didn't enable optimizations (e.g. -O2).



      From my tests it looks like gcc always recognizes strcmp with constant arguments and optimizes it, even with -O0 (no optimizations). Clang needs at least -O1 to do so.



      That's where the difference comes from: The code produced by clang calls strcmp twice, but the code produced by gcc just does printf("%dn", 1) in the first case because it knows that 'h' > 'H' (ASCIIbetically, that is). It's just constant folding, really.



      Live example: https://godbolt.org/z/8Hg-gI



      As the other answers explain, any positive value will do to indicate that the first string is greater than the second, so the compiler optimizer simply chooses 1. The strcmp library function apparently uses a different value.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 1 hour ago









      melpomene

      50.7k53682




      50.7k53682







      • 2




        Although it's interesting that Clang and GCC can be induced to compile the program either such that their respective results produce the same output or such that they don't, I don't like interpreting that as optimization or lack thereof being the reason for the output to differ. It would be better to generalize that to "implementation details", as optimization is only one reason why the results might differ, whether in this specific case or (even more so) in the general case.
        – John Bollinger
        1 hour ago










      • You can also use -fbo-builtin to observe some of these effects
        – Shafik Yaghmour
        20 mins ago












      • 2




        Although it's interesting that Clang and GCC can be induced to compile the program either such that their respective results produce the same output or such that they don't, I don't like interpreting that as optimization or lack thereof being the reason for the output to differ. It would be better to generalize that to "implementation details", as optimization is only one reason why the results might differ, whether in this specific case or (even more so) in the general case.
        – John Bollinger
        1 hour ago










      • You can also use -fbo-builtin to observe some of these effects
        – Shafik Yaghmour
        20 mins ago







      2




      2




      Although it's interesting that Clang and GCC can be induced to compile the program either such that their respective results produce the same output or such that they don't, I don't like interpreting that as optimization or lack thereof being the reason for the output to differ. It would be better to generalize that to "implementation details", as optimization is only one reason why the results might differ, whether in this specific case or (even more so) in the general case.
      – John Bollinger
      1 hour ago




      Although it's interesting that Clang and GCC can be induced to compile the program either such that their respective results produce the same output or such that they don't, I don't like interpreting that as optimization or lack thereof being the reason for the output to differ. It would be better to generalize that to "implementation details", as optimization is only one reason why the results might differ, whether in this specific case or (even more so) in the general case.
      – John Bollinger
      1 hour ago












      You can also use -fbo-builtin to observe some of these effects
      – Shafik Yaghmour
      20 mins ago




      You can also use -fbo-builtin to observe some of these effects
      – Shafik Yaghmour
      20 mins ago












      up vote
      6
      down vote













      The standard defines the result of strcmp to be negative, if lhs appears before rhs in lexical order, zero if they are equal, or a positive value if lhs appears lexically after rhs.



      It's up to the implementation how to implement that and what exactly to return. You must not depend on a specific value in your programs, or they won't be portable. Simply check with comparisons (<, >, ==).



      See https://en.cppreference.com/w/c/string/byte/strcmp



      Background



      One simple implementation might just calculate the difference of each character c1 - c2 and do that until the result is not zero, or one of the strings ends. The result will then be the numeric difference between the first character, in which the two strings differed.



      For example, this GLibC implementation: https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=string/strcmp.c;hb=HEAD






      share|improve this answer


























        up vote
        6
        down vote













        The standard defines the result of strcmp to be negative, if lhs appears before rhs in lexical order, zero if they are equal, or a positive value if lhs appears lexically after rhs.



        It's up to the implementation how to implement that and what exactly to return. You must not depend on a specific value in your programs, or they won't be portable. Simply check with comparisons (<, >, ==).



        See https://en.cppreference.com/w/c/string/byte/strcmp



        Background



        One simple implementation might just calculate the difference of each character c1 - c2 and do that until the result is not zero, or one of the strings ends. The result will then be the numeric difference between the first character, in which the two strings differed.



        For example, this GLibC implementation: https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=string/strcmp.c;hb=HEAD






        share|improve this answer
























          up vote
          6
          down vote










          up vote
          6
          down vote









          The standard defines the result of strcmp to be negative, if lhs appears before rhs in lexical order, zero if they are equal, or a positive value if lhs appears lexically after rhs.



          It's up to the implementation how to implement that and what exactly to return. You must not depend on a specific value in your programs, or they won't be portable. Simply check with comparisons (<, >, ==).



          See https://en.cppreference.com/w/c/string/byte/strcmp



          Background



          One simple implementation might just calculate the difference of each character c1 - c2 and do that until the result is not zero, or one of the strings ends. The result will then be the numeric difference between the first character, in which the two strings differed.



          For example, this GLibC implementation: https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=string/strcmp.c;hb=HEAD






          share|improve this answer














          The standard defines the result of strcmp to be negative, if lhs appears before rhs in lexical order, zero if they are equal, or a positive value if lhs appears lexically after rhs.



          It's up to the implementation how to implement that and what exactly to return. You must not depend on a specific value in your programs, or they won't be portable. Simply check with comparisons (<, >, ==).



          See https://en.cppreference.com/w/c/string/byte/strcmp



          Background



          One simple implementation might just calculate the difference of each character c1 - c2 and do that until the result is not zero, or one of the strings ends. The result will then be the numeric difference between the first character, in which the two strings differed.



          For example, this GLibC implementation: https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=string/strcmp.c;hb=HEAD







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 1 hour ago









          Benjamin Maurer

          1,112824




          1,112824




















              up vote
              5
              down vote













              The strcmp function is only specified to return a value larger than zero, zero, or less than zero. There's nothing specified what those positive and negative values have to be.






              share|improve this answer
























                up vote
                5
                down vote













                The strcmp function is only specified to return a value larger than zero, zero, or less than zero. There's nothing specified what those positive and negative values have to be.






                share|improve this answer






















                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  The strcmp function is only specified to return a value larger than zero, zero, or less than zero. There's nothing specified what those positive and negative values have to be.






                  share|improve this answer












                  The strcmp function is only specified to return a value larger than zero, zero, or less than zero. There's nothing specified what those positive and negative values have to be.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  Some programmer dude

                  282k23231385




                  282k23231385




















                      up vote
                      3
                      down vote













                      The exact values returned by strcmp in the case of the strings not being equal are not specified. From the man page:




                      #include <string.h>
                      int strcmp(const char *s1, const char *s2);
                      int strncmp(const char *s1, const char *s2, size_t n);


                      The strcmp() and strncmp() functions return an integer less than,
                      equal to, or greater than zero if s1 (or the first n bytes thereof) is
                      found, respectively, to be less than, to match, or be greater than s2.




                      Since str1 compares greater than str2, the value must be positive, which it is in both cases.



                      As for the difference between the two compilers, it appears that clang is returning the difference between the ASCII values for the corresponding characters that mismatched, while gcc is opting for a simple -1, 0, or 1. Both are valid, so your code should only need to check if the value is 0, greater than 0, or less than 0.






                      share|improve this answer
















                      • 1




                        The interesting thing is that gcc only gave 1 when passing in the string literals. I suspect it may have been an optimization knowing that the result would always be the same.
                        – Christian Gibbons
                        1 hour ago














                      up vote
                      3
                      down vote













                      The exact values returned by strcmp in the case of the strings not being equal are not specified. From the man page:




                      #include <string.h>
                      int strcmp(const char *s1, const char *s2);
                      int strncmp(const char *s1, const char *s2, size_t n);


                      The strcmp() and strncmp() functions return an integer less than,
                      equal to, or greater than zero if s1 (or the first n bytes thereof) is
                      found, respectively, to be less than, to match, or be greater than s2.




                      Since str1 compares greater than str2, the value must be positive, which it is in both cases.



                      As for the difference between the two compilers, it appears that clang is returning the difference between the ASCII values for the corresponding characters that mismatched, while gcc is opting for a simple -1, 0, or 1. Both are valid, so your code should only need to check if the value is 0, greater than 0, or less than 0.






                      share|improve this answer
















                      • 1




                        The interesting thing is that gcc only gave 1 when passing in the string literals. I suspect it may have been an optimization knowing that the result would always be the same.
                        – Christian Gibbons
                        1 hour ago












                      up vote
                      3
                      down vote










                      up vote
                      3
                      down vote









                      The exact values returned by strcmp in the case of the strings not being equal are not specified. From the man page:




                      #include <string.h>
                      int strcmp(const char *s1, const char *s2);
                      int strncmp(const char *s1, const char *s2, size_t n);


                      The strcmp() and strncmp() functions return an integer less than,
                      equal to, or greater than zero if s1 (or the first n bytes thereof) is
                      found, respectively, to be less than, to match, or be greater than s2.




                      Since str1 compares greater than str2, the value must be positive, which it is in both cases.



                      As for the difference between the two compilers, it appears that clang is returning the difference between the ASCII values for the corresponding characters that mismatched, while gcc is opting for a simple -1, 0, or 1. Both are valid, so your code should only need to check if the value is 0, greater than 0, or less than 0.






                      share|improve this answer












                      The exact values returned by strcmp in the case of the strings not being equal are not specified. From the man page:




                      #include <string.h>
                      int strcmp(const char *s1, const char *s2);
                      int strncmp(const char *s1, const char *s2, size_t n);


                      The strcmp() and strncmp() functions return an integer less than,
                      equal to, or greater than zero if s1 (or the first n bytes thereof) is
                      found, respectively, to be less than, to match, or be greater than s2.




                      Since str1 compares greater than str2, the value must be positive, which it is in both cases.



                      As for the difference between the two compilers, it appears that clang is returning the difference between the ASCII values for the corresponding characters that mismatched, while gcc is opting for a simple -1, 0, or 1. Both are valid, so your code should only need to check if the value is 0, greater than 0, or less than 0.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 1 hour ago









                      dbush

                      82.4k1088119




                      82.4k1088119







                      • 1




                        The interesting thing is that gcc only gave 1 when passing in the string literals. I suspect it may have been an optimization knowing that the result would always be the same.
                        – Christian Gibbons
                        1 hour ago












                      • 1




                        The interesting thing is that gcc only gave 1 when passing in the string literals. I suspect it may have been an optimization knowing that the result would always be the same.
                        – Christian Gibbons
                        1 hour ago







                      1




                      1




                      The interesting thing is that gcc only gave 1 when passing in the string literals. I suspect it may have been an optimization knowing that the result would always be the same.
                      – Christian Gibbons
                      1 hour ago




                      The interesting thing is that gcc only gave 1 when passing in the string literals. I suspect it may have been an optimization knowing that the result would always be the same.
                      – Christian Gibbons
                      1 hour ago










                      fips197 is a new contributor. Be nice, and check out our Code of Conduct.









                       

                      draft saved


                      draft discarded


















                      fips197 is a new contributor. Be nice, and check out our Code of Conduct.












                      fips197 is a new contributor. Be nice, and check out our Code of Conduct.











                      fips197 is a new contributor. Be nice, and check out our Code of Conduct.













                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52334056%2fweird-return-value-in-strcmp%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                      Confectionery