Thread: if(!strcmp(str1, str2)

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    54

    if(!strcmp(str1, str2)

    Hi!

    It looks a bit tricky to me, so please let me know if I understand

    this piece of code correctly:

    if(!strcmp(str1,str2);

    strcmp returns 0 if str1 = str2
    strcmp returns 1 if str1 > str2
    strcmp returns -1 if str1 < str2

    so, if(!strcmp(str1,str2) actually sets strcmp to 1, i.e. to true;

    because when strcmp returns 0, meaning that str1 = str2,

    if I use "if(strcmp(str1,str2)" I will get "false" as the result

    (because strcmp(str1, str2) will equal 0); by negating it

    I actually get "true" as a result, and if(!strcmp(str1,str2)

    means that both strings are equal. Correct?

    When strcmp(str1,str2) returns 1, by negating it I am puting

    the condition to false, so if(!strcmp(str1,str2) has the condition

    of 0, and thus false. Correct?

    If the above is correct, what happens when I get strcmp(str1,str2) = -1?

    Thanks a lot!

  2. #2
    Registered User
    Join Date
    May 2015
    Posts
    56
    https://www.le.ac.uk/users/rjm1/cotter/page_37.htm
    In C true is represented by any numeric value not equal to 0 and false is represented by 0.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I don't recommend trying to use the return value of strcmp() as a Boolean value. Do the comparison you actually want, your logic will be clearer.

    Code:
    if(strcmp(str1, str2) ==  0)
      // Do something.
    else if(strcmp(str1, str2) == 1)
      // Do something else.
    else if(strcmp(str1, str2) == -1)
      // Do something else if string 1 is less than str2.
    Remember in C "false" is equal to zero, "true" is everything else.

    Jim

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You seem to have two confusions.

    Firstly, if str1 > str2, strcmp returns a positive number, not necessarily 1. If str1 < str2, strcmp returns a negative number, not necessarily -1. If they're equal, strcmp returns 0 like you said.

    Secondly, in C, 0 is false and any other number is true. So both 1 and -1 (or -123456, for that matter) are "true" in a boolean context.

    Best practice for using strcmp is to compare it to 0 and not use the ! operator:
    Code:
    if (strcmp(s1, s2) == 0) {
        // they're equal
    }
    
    if (strcmp(s1, s2) < 0)) {
        // s1 < s2
    }

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    54
    Quote Originally Posted by algorism View Post
    You seem to have two confusions.

    Firstly, if str1 > str2, strcmp returns a positive number, not necessarily 1. If str1 < str2, strcmp returns a negative number, not necessarily -1. If they're equal, strcmp returns 0 like you said.

    Secondly, in C, 0 is false and any other number is true. So both 1 and -1 (or -123456, for that matter) are "true" in a boolean context.

    Best practice for using strcmp is to compare it to 0 and not use the ! operator:
    Code:
    if (strcmp(s1, s2) == 0) {
        // they're equal
    }
    
    if (strcmp(s1, s2) < 0)) {
        // s1 < s2
    }
    Thank you for your answer. I was quoting the code I have seen and the one I have to understand,

    that is why I have asked. Do I assume correctly that if str1 = str2, then if(!strcmp(str1, str2) means true?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducol
    Do I assume correctly that if str1 = str2, then if(!strcmp(str1, str2) means true?
    To put it even more plainly, if the strings are equal, then strcmp returns 0, and since !0 == 1, yes, !strcmp(str1, str2) evaluates to true. You already figured that out in your first post.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what exactly does strcmp() do?
    By MattJ812 in forum C++ Programming
    Replies: 6
    Last Post: 01-16-2011, 11:02 PM
  2. Difference between char *str1 & const char *str2
    By Tigers! in forum C Programming
    Replies: 4
    Last Post: 08-06-2009, 04:04 AM
  3. strcmp()
    By norhos in forum C Programming
    Replies: 15
    Last Post: 03-15-2008, 09:15 AM
  4. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM
  5. need function like strncmp(str1, str2, n)
    By evader in forum C Programming
    Replies: 5
    Last Post: 09-23-2001, 08:42 PM

Tags for this Thread