delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/04/18:17:16

Sender: nate AT cartsys DOT com
Message-ID: <377FDD3D.A40E4675@cartsys.com>
Date: Sun, 04 Jul 1999 15:16:29 -0700
From: Nate Eldredge <nate AT cartsys DOT com>
X-Mailer: Mozilla 4.08 [en] (X11; I; Linux 2.2.10 i586)
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: Newbie problems... Whats wrong with this code?
References: <Q4xf3.524$nk6 DOT 2262 AT news1 DOT online DOT no> <931066419 DOT 823 DOT 65 AT news DOT remarQ DOT com>
Reply-To: djgpp AT delorie DOT com

Al Morgan wrote:

> The function you need in strcmp(char *str1, char *str2);  It returns 0 if
> both strings are equal (counter intuitive, isn't it?).

I find it helps to think of it as subtraction.  i.e. strcmp(s1, s2) <=>
(s1 - s2).  This also helps keep straight the meaning of the sign of the
result (which one is greater), in the rare instances where one cares.

I personally prefer to write `if (strcmp(s1, s2) == 0) ...' instead of
`if (!strcmp(s1, s2))' for this reason, since it helps me think of
strcmp as an arithmetic rather than logical operation.  Even though it
is a few more bytes.

-- 

Nate Eldredge
nate AT cartsys DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019