Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > K&R2, page 106 ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 3 Topic 25480 of 27671
Post > Topic >>

K&R2, page 106 <strcmp>

by arnuld <arnVuld@[EMAIL PROTECTED] > Apr 10, 2008 at 01:11 PM

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

  http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_5:Exercise_4


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("comparing <%s> with <%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 ok> with <nope> = -5
/home/arnuld/programs/C $ 


-- 
http://lispmachine.wordpress.com/

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




 3 Posts in Topic:
K&R2, page 106
arnuld <arnVuld@[EMAIL  2008-04-10 13:11:25 
Re: K&R2, page 106
WANG Cong <xiyou.wangc  2008-04-10 16:05:29 
Re: K&R2, page 106
"Bartc" <bc@  2008-04-10 08:25:24 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 8:50:25 CDT 2008.