This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

strcmp

In the example provided here, the strings are of different. I just wanted to ask what happens when at some index the array elements differ

as in

char * str1 = "Hello";
and
char * str2 = "Helio";
and I do
j = strcmp(str1, str2);

Will strcmp return the difference of ASCII's of the differing characters? i.e.
j = str1[3]-str2[3] in the above case

Parents
  • "Less in length? Less in what?"

    Less lexicographically.

    Here's the source from a well known C library implementation:

    int strcmp(char * src, char * dest)
    {
       int ret = 0 ;
    
       while(!(ret=*(unsigned char *)src-
       *(unsigned char *)dest) && *dest)
       {
          ++src;
          ++dest;
       }
    
       if(ret<0)
          ret = -1 ;
       else
          if(ret>0)
             ret=1;
    
       return(ret);
    }
    

Reply
  • "Less in length? Less in what?"

    Less lexicographically.

    Here's the source from a well known C library implementation:

    int strcmp(char * src, char * dest)
    {
       int ret = 0 ;
    
       while(!(ret=*(unsigned char *)src-
       *(unsigned char *)dest) && *dest)
       {
          ++src;
          ++dest;
       }
    
       if(ret<0)
          ret = -1 ;
       else
          if(ret>0)
             ret=1;
    
       return(ret);
    }
    

Children