Thread: if(strcmp(string1, string2)) Equivalent to if(strcmp(string1, string2) == 0)

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    if(strcmp(string1, string2)) Equivalent to if(strcmp(string1, string2) == 0)

    Is this statement:

    if(strcmp(string1, string2))

    Equivalent to

    if(strcmp(string1, string2) == 0)

    My understanding is case 1 is the correct way to execute an if statement if two strings match. In my code however, case 1 produces an error and case 2 gives the behaviour that I want.

    This is an SSCE excerpt. My entire codebase is pretty large.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,731
    No, you are mistaken:
    Code:
    if (A)
    // is equivalent to
    if (A != 0)
    In C, zero is false and everything else is true.
    Devoted my life to programming...

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    746
    Quote Originally Posted by GReaper View Post
    No, you are mistaken:
    Code:
    if (A)
    // is equivalent to
    if (A != 0)
    In C, zero is false and everything else is true.
    Actually, you are partially mistaken.
    Code:
    if(a) { ... }
    Yes, if a != 0 then a is "true", and if a == 0, then a is "false", in this expression.

    Code:
    if(strcmp(s1, s2))
    However, your final statement is not always true.

    if you "man strcmp", then you will see:

    s1 is less than s2, strcmp() returns an int less than 0
    s1 is equal to s2, strcmp() returns 0
    s1 is greater than s2, strcmp() returns an int greater than 0

    In the OP, the two statements are not the same. Plus you really need to code to handle the three cases.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    However, your final statement is not always true.
    In what way? Yes, you went on to say that strcmp's return value has a complex meaning, but that doesn't take away from the fact that integer value 0 is a Boolean false, and other integer values are Boolean true, which would make the statement you objected to... true.

    In the OP, the two statements are not the same. Plus you really need to code to handle the three cases.
    Perhaps this is true if you think, for example, that if(strcmp(s1,s2) == 0) ... handles all three cases. It's the same as saying that s1 being lexicographically equal to s2 is right and other results are wrong. In my experience, being interested in one result of strcmp is the overwhelming general case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare if string1 has same letter with string2
    By John Roble in forum C Programming
    Replies: 14
    Last Post: 08-02-2017, 10:03 AM
  2. C++ Map vs. strcmp
    By Scarvenger in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2007, 11:24 AM
  3. strcmp help!!
    By apm in forum C Programming
    Replies: 14
    Last Post: 06-07-2007, 06:54 PM
  4. strcmp???
    By SvNo7 in forum C Programming
    Replies: 7
    Last Post: 12-30-2006, 04:34 PM
  5. What does strcmp actually do?
    By Brewer in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 01:32 PM

Tags for this Thread