# crazy doubt!

• 06-06-2006
Shal
crazy doubt!
I was wondering tht the normal way of doing things is, if some test passes then the value is true. Say for e.g if u use functions like isalpha() or isdigit() etc.. then the value returned is true if the character is an alphabet or a digit...

Y then string functions like strcmp(s1, s2) return true if the strings are not equal? I keep wondering y they designed the string functions like that?
• 06-06-2006
twomers
Why? I don't know, but I think with strcmp(), the 'true' value is 0, so it may imply that there are 0 differences between the first and second string, but they're the only ones I know of which do that.
• 06-06-2006
Prelude
>Y then string functions like strcmp(s1, s2) return true if the strings are not equal?
They don't. They return one of three values depending on the relationship between the two strings. The value is negative if the first string is lexographically less than the second, 0 if the strings are equal, and positive if the first string is lexographically greater than the second. The result does not represent a boolean value, so thinking in terms of true and false will only confuse you.
• 06-06-2006
Mario F.
Mind you, strcmp() returns one of 3 possible values. It doesn't test only for one of two.

int result = strcmp(string str1, string str2);

if str1 less than str2, returns a negative value
if str1 = str2, returns 0
if str1 > str2, returns a positive value

Now, if you look at similar functions that return one of three possible values, you will find that the same convention follows. And also, they don't return bool, for the obvious reasons.
• 06-06-2006
Shal
Thanks . solves my problem...