Why strcmp equals zero

This is a discussion on Why strcmp equals zero within the C Programming forums, part of the General Programming Boards category; I followed Hammer's tutorial on Comparing Strings, but one thing I don't understand. Code: if(strcmp(foo, bar) == 0) puts("Strings equal"); ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    26

    Why strcmp equals zero

    I followed Hammer's tutorial on Comparing Strings, but one thing I don't understand.

    Code:
    if(strcmp(foo, bar) == 0)
      puts("Strings equal");
    else
     puts("Strings not equal");
    Why compare these strings to zero. I know that in boolean 0 is false and 1 is true. I have to say that these tutorials are very good for beginners.

    Thanks.

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    15
    string comparison starts with the first character and goes on till a character differs or one of the strings gets completed
    coonsider strcmp(s1,s2)
    if strcmp(s1,s2) == 0 => s1==s2
    if strcmp(s1,s2) <0 => s1<s2
    ifstrcmp(s1,s2) > 0 => s1>s2

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    15
    oh and the value strcmp returns is the diff. betwn. the value of the corres. characters of the strings. //pls. verify this

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    Somehow I got the code for this (and many other c functions)

    -edit-
    Ok, they're all the source code for C functions....in C. Some of the functions are written in assembly, but I don't feel like finding the one's I've seen. It's just nice to see how they did the actual functions written out.

    anyways
    Code:
         
         /***
         *strcmp - compare two strings, returning less than, equal to, or greater than
         *
         *Purpose:
         *	   STRCMP compares two strings and returns an integer
         *	   to indicate whether the first is less than the second, the two are
         *	   equal, or whether the first is greater than the second.
         *
         *	   Comparison is done byte by byte on an UNSIGNED basis, which is to
         *	   say that Null (0) is less than any other character (1-255).
         *
         *Entry:
         *	   const char * src - string for left-hand side of comparison
         *	   const char * dst - string for right-hand side of comparison
         *
         *Exit:
         *	   returns -1 if src <  dst
         *	   returns  0 if src == dst
         *	   returns +1 if src >  dst
         *
         *Exceptions:
         *
         *******************************************************************************/
         
         int __cdecl strcmp (
         		const char * src,
         		const char * dst
         		)
         {
         		int ret = 0 ;
         
     		while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
         				++src, ++dst;
         
         		if ( ret < 0 )
         				ret = -1 ;
         		else if ( ret > 0 )
         				ret = 1 ;
         
         		return( ret );
         }
    Last edited by jverkoey; 07-21-2004 at 09:42 PM.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    To reiterate what alvarorahul said:

    strcmp() compares the strings one character at a time. If the characters match it goes on to the next character. Once it finds a difference it subtracts the character from string2 from the character from string1.

    So given:
    Code:
    char s1[] = "My name is Bob";
    char s2[] = "My name is Billy";
    It will compare "My name is B" from both strings and then read o from s1 and i from s2 and then returns the value of o-i. If the value returned is < 0 then s1 is less than s2, if the value returned is > 0 then s1 is greater then s2. So by default (only range left) 0 means they are the same.

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    strcmp is really helpful in sorting algorithms.

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, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 05:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 01:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 12:04 PM

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