Thread: what exactly does strcmp() do?

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    48

    what exactly does strcmp() do?

    Code:
    int strcmp ( const char *s1, const char *s2 );
    strcmp will accept two strings. It will return an integer. This integer will either be:

    Negative if s1 is less than s2.
    Zero if s1 and s2 are equal.
    Positive if s1 is greater than s2.
    What exactly is this checking? How is 'John' less, more than or equal to 'Joey'...I guess is my question.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Lookup John and Joey in a dictionary.

    < is "John is before Joey" in the dictionary
    > is "John is after Joey" in the dictionary

    = 0 means that they are the same word.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    48
    Obviously, so it checks which comes first in the dictionary?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Not quite: it compares null terminated strings similiar to how you could use a dictionary to compare words.
    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

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It performs a "lexicographical" comparison. Google that for more info
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    48
    Quote Originally Posted by iMalc View Post
    It performs a "lexicographical" comparison. Google that for more info
    Perfect thanks.

  7. #7
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Or use the man page.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to read in octal
    By laxkrzy in forum C Programming
    Replies: 6
    Last Post: 11-08-2010, 11:34 PM
  2. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  3. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  4. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM