help with strcmp()

This is a discussion on help with strcmp() within the C Programming forums, part of the General Programming Boards category; Can someone how the following line works step by step? I know what it does, but I guess I don't ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    help with strcmp()

    Can someone how the following line works step by step? I know what it does, but I guess I don't understand the syntax.

    Code:
    return strcmp(word, "STOP") ! = 0;
    The line is supposed to return 0 if the string word = STOP, and return 1 if thats not true. I dont understand how or why though.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It returns the truth value (which in C is an int 0-false nonzero-true) of the statement strcmp(word,"STOP") != 0;

    If word contains exactly "STOP" then strcmp(word,"STOP") evaluates to 0 and strcmp(word,"STOP") != 0 evaluates to false-0, so the 0 is the value returned.

    If word is differet from "STOP" then strcmp(word,"STOP") returns some non-zero value and strcmp(word,"STOP") != 0 evaluates to true-nonzero, so a nonzero value is returned.

    It's the same as saying this:

    Code:
    int result = strcmp(word,"STOP");
    return result != 0;

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't understand the need for the != 0 part, at all. You can use the return from strcmp() as a bool (true false) expression, just as it is, without that part being tacked on to it.

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Quote Originally Posted by Adak View Post
    I don't understand the need for the != 0 part, at all.
    Some people (not me) think it adds clarity to the code. Maybe it was written by a programmer of a language that doesn't convert ints to bools.

    This doesn't really both me, but sometimes I see java programmers write ridiculous crap like:
    Code:
    if ((a!=0)==true){
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Because strcmp() returns an integer, you must translate it to a bool value in C++ (and in C99?). Leaving type casting to compiler will be dangerous.

    strcmp() returns false if strings are equal for example while you may expect it to return true in this case.

    Code:
    if ((a!=0)==true){
    It is the exact code you should write if "a" is not bool, in any language the bool type is defined in it.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,408
    Quote Originally Posted by Adak
    I don't understand the need for the != 0 part, at all. You can use the return from strcmp() as a bool (true false) expression, just as it is, without that part being tacked on to it.
    I think that it makes sense. strcmp(a, b) == 0 tests if a and b are equal. strcmp(a, b) != 0 tests if a and b and not equal. strcmp(a, b) < 0 tests if a is less than b, etc. On the other hand, !strcmp(a, b) tests for... a is not equal to b? Oops.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by laserlight View Post
    I think that it makes sense. strcmp(a, b) == 0 tests if a and b are equal. strcmp(a, b) != 0 tests if a and b and not equal. strcmp(a, b) < 0 tests if a is less than b, etc. On the other hand, !strcmp(a, b) tests for... a is not equal to b? Oops.
    I think it's the return statement that makes things a bit confusing.

  8. #8
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Quote Originally Posted by siavoshkc View Post
    Code:
    if ((a!=0)==true){
    It is the exact code you should write if "a" is not bool, in any language the bool type is defined in it.
    No it isn't. Even if you can't implicitly convert an int to a bool, (a!=0) is a bool, so the (...==true) part is useless.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Quote Originally Posted by NeonBlack View Post
    No it isn't. Even if you can't implicitly convert an int to a bool, (a!=0) is a bool, so the (...==true) part is useless.
    You know, you are right.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 05:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 07:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM

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