K&R2, page 106 <strcmp>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • arnuld

    K&R2, page 106 <strcmp>

    I am suing the strcmp function from K&R2, page 106 which is used here:




    I only changed the name to avoid confusion with real strcmp. K&R2 says:
    it returns < 0, if s <t, 0 if s== t, 0 if s t.

    but I have found, it returns < 0 if s t and vice-versa.


    int my_strcmp( char* s, char* t )
    {
    for(; *s == *t; ++s, ++t )
    {
    printf("*s = %c\n", *s);
    printf("*t = %c\n", *t);

    if( *s == '\0' )
    {
    return 0;
    }
    }

    return *s - *t;
    }



    int main(void)
    {
    char s[] = "it is ok";
    char t[] = "nope";

    printf("compari ng <%swith <%s= %d\n", s, t, my_strcmp( s, t ));

    return 0;

    }

    =========== OUTPUT ============
    /home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra 5-5.c
    /home/arnuld/programs/C $ ./a.out
    comparing <it is okwith <nope= -5
    /home/arnuld/programs/C $


    --


    Please remove capital 'V's when you reply to me via e-mail.

  • WANG Cong

    #2
    Re: K&amp;R2, page 106 &lt;strcmp&g t;

    arnuld wrote:
    I am suing the strcmp function from K&R2, page 106 which is used here:
    >

    >
    >
    I only changed the name to avoid confusion with real strcmp. K&R2 says:
    it returns < 0, if s <t, 0 if s== t, 0 if s t.
    >
    but I have found, it returns < 0 if s t and vice-versa.
    >
    >
    int my_strcmp( char* s, char* t )
    {
    for(; *s == *t; ++s, ++t )
    {
    printf("*s = %c\n", *s);
    printf("*t = %c\n", *t);
    >
    if( *s == '\0' )
    {
    return 0;
    }
    }
    >
    return *s - *t;
    }
    >
    >
    >
    int main(void)
    {
    char s[] = "it is ok";
    char t[] = "nope";
    >
    printf("compari ng <%swith <%s= %d\n", s, t, my_strcmp( s, t ));
    >
    return 0;
    >
    }
    's' is definitely less than 't' here.

    --
    Hi, I'm a .signature virus, please copy/paste me to help me spread
    all over the world.

    Comment

    Working...