Thread: strcmp return value on different compilers

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58

    strcmp return value on different compilers

    Hello there, I'm having a problem with the strcmp return value. My compiler returns the correct value based on my research, but I tried to compile the code below on those online compilers and the result was 1.

    My compiler gave me 10, which I consider to be the correct result according to strcmp - C++ Reference.

    Can somebody explain this difference?
    So strcmp is not very portable?

    Here's the websites I used to test it:
    C code - 8 lines - codepad
    Compile and Execute Programs Online| Online IDE

    Thanks in advance.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        printf("Return value: %d\n", strcmp("Rocks", "Rockie"));
        
        return 0;
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Quote Originally Posted by cplusplus
    A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2
    Did you get a different result?

    gg

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    From your own link:

    A zero value indicates that both strings are equal.
    A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.
    It doesn't matter what the actual numeric value is - the standard says nothing about that. The only information in the answer is whether the return is greater, equal, or less than 0.

    Both 1 and 10 are greater than 0, indicating str1 is greater than str2 - and it is, because s is further in the alphabet than i. Both are acceptable return values. It could return 1, or 10, or 32441, it doesn't matter - it returns something > 0.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So strcmp is not very portable?
    No strcmp() is very portable. You're just using it wrong.

    Jim

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58
    Sorry, I misunderstood that part.

    So my compiler returning how bigger or how smaller the value is, is just a plus and not a guaranteed result?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So my compiler returning how bigger or how smaller the value is, is just a plus and not a guaranteed result?
    Correct, the standard only guarantees zero, greater than zero, or less than zero.

    Jim

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58
    Thanks everybody and sorry for the misinterpretation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp return value
    By cph in forum C Programming
    Replies: 20
    Last Post: 01-28-2011, 11:52 AM
  2. what does strcmp return?
    By nullifyed in forum C Programming
    Replies: 4
    Last Post: 06-19-2010, 05:29 PM
  3. Replies: 1
    Last Post: 07-04-2007, 12:20 AM
  4. Replies: 12
    Last Post: 04-07-2007, 11:11 AM
  5. Replies: 6
    Last Post: 04-09-2006, 04:32 PM