Is this if/strcmp correct?

This is a discussion on Is this if/strcmp correct? within the C Programming forums, part of the General Programming Boards category; Will this if statement be correctly evaluated? Or is it incorrect....it doesn't seem to work but it may be something ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    62

    Is this if/strcmp correct?

    Will this if statement be correctly evaluated? Or is it incorrect....it doesn't seem to work but it may be something else.

    Thanks

    Code:
    if ((strcmp (procName,"firstname") != 0) || (strcmp (procName,"surname") != 0) || (strcmp (procName,"middlename") != 0))

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    strcmp returns 0 if equal.

    Use ==, not !=

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    62
    Quote Originally Posted by Tonto
    strcmp returns 0 if equal.

    Use ==, not !=
    I only want it to continue past if it does not find the string...so should it be

    == 1

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Nope. strcmp has three different return values. Read the man page.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    By "continue past", you mean not do the code in the scope of the if statement, correct?

    If that's the case, then you'd want == because, if there is no match, then that statement would return true and go into the if scope.

    ...and no, comparing to 1 would be incorrect because strcmp() doesn't return 1 when they don't match it returns the difference of the bytes. In other words, it could be any number that isn't 0.
    Last edited by SlyMaelstrom; 02-21-2006 at 10:25 PM.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    62
    Quote Originally Posted by SlyMaelstrom
    By "continue past", you mean not do the code in the scope of the if statement, correct?

    If that's the case, then you'd want == because, if there is no match, then that statement would return true and go into the if scope.

    ...and no, comparing to 1 would be incorrect because strcmp() doesn't return 1 when they don't match it returns the difference of the bytes. In other words, it could be any number that isn't 0.

    OK...thanks for all of the help

    This is what I want to do:

    if x isn't "x" or "y" or "z" then
    ....
    else
    .....

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well then, it should be correctly evaluated.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No it won't. You have to use and. Pass it "surname" and watch it fail on the first check.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    ...It won't fail because strcmp() won't return 0. And TRUE || FALSE || TRUE returns TRUE and enters the statement.
    Sent from my iPadŽ

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes it will fail. It will fail to do what they said they wanted it to. It will fail on the first test, and enter the if statement if anything other than "firstname" is entered for the first check. They said they want it to "continue past" if blah blah. Well it won't. It will always enter the first loop. Don't believe me?
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main( void )
    {
        char *foo[] = { "firstname", "surname", "middlename" };
        size_t x;
    
        for( x = 0; x < sizeof( foo ) / sizeof( foo[0] ); x++ )
            if ( (strcmp ( foo[ x ],"firstname")  != 0)
            ||   (strcmp ( foo[ x ],"surname")    != 0)
            ||   (strcmp ( foo[ x ],"middlename") != 0) )
            {
                printf("Failed.\n");
            }
        
        return 0;
    }
    Read'm and weep.

    Like I said, you have to use and. The confusion comes from the OP's wording. The test doesn't "fail" as in evaluate to false always. It fails in the sense that it will never do what they want it to do.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    62
    Would I be better off using strstr?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    /bonk

    You're missing the point entirely. Your check is wrong. Anything you supply as an argument will run the body of the if. There is no way to avoid it. Changing to strstr isn't going to fix the problem. In fact, even though it's not really possible, it'll make it even "more broken".

    Why is it no one ever listens to what I say around here? Hell, I didn't even chew anyone out in this thread yet.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    You guys are really dumb. Arguing on a topic only a beginner has problems with. You've argued to so much you've even confused me quzah is right. When you say, "...isn't blah or blah or blah..." you're really using the AND symbol because all of the conditions have to be met.

    Oh, and one more thing. The AND symbol is like this: &&. One & is a bitwise AND.

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by quzah
    It will fail on the first test, and enter the if statement if anything other than "firstname" is entered for the first check. They said they want it to "continue past" if blah blah. Well it won't. It will always enter the first loop. Don't believe me?
    ...
    Read'm and weep.
    *yawn*

    I'm going to cherish the moment.

    If you reread the posts, you'll see that I asked what the OP meant by "continue past". He then replied with this:

    Code:
    if x isn't "x" or "y" or "z" then
    ....
    else
    .....
    Implying HE WANTS TO ENTER THE STATEMENT if the variable isn't one of the three literals. I also understood "continue past" to skip over the if, then he said he meant to enter into the if. There for, the condition should be correct.
    Sent from my iPadŽ

  15. #15
    Registered User
    Join Date
    Jan 2006
    Posts
    62
    Sorry for causing all of this...as you can see I am a beginner and yes this is what I'm wanting

    Code:
    if x isn't "x" or "y" or "z" then
    printf("%s isn't the same as x or y or z",x)
    else
    printf("%s is the same as x or y or z",x)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux for GNU/Linux is not correct?
    By password636 in forum Linux Programming
    Replies: 8
    Last Post: 03-31-2009, 08:30 PM
  2. Is this correct : passing strings?
    By socket in forum C Programming
    Replies: 15
    Last Post: 11-25-2008, 01:03 PM
  3. correct malloc use
    By s_siouris in forum C Programming
    Replies: 10
    Last Post: 05-28-2008, 10:49 PM
  4. Replies: 1
    Last Post: 05-26-2004, 12:58 AM
  5. Why I couldn't see the correct format of a file?
    By miketv in forum C Programming
    Replies: 2
    Last Post: 01-23-2002, 09:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21