tags:

views:

489

answers:

3

I'm trying to do exercise 5-4 in the K&R C book. I have written the methods for strncpy and strncat, but I'm having some trouble understanding exactly what to return for the strncmp part of the exercise.

The definition of strncmp (from Appendix B in K&R book) is:

compare at most n characters of string s to string t; return <0 if s<t, 0 if s==t, or >0 if s>t

Lets say I have 3 strings:

char s[128] = "abc"
char t[128] = "abcdefghijk"
char u[128] = "hello"

And I want to compare them using the strncmp function I have to write. I know that

strncmp(s, t, 3)

will return 0 ,because abc == abc. Where I'm confused is the other comparisons. For example

strncmp(s, t, 5) and
strncmp(s, u, 4)

The first matches up the 3th position and then after that they no longer match and the second example doesn't match at all.

I really just want know what those 2 other comparisons return and why so that I can write my version of strncmp and finish the exercise.

+2  A: 

Both return a negative number (it just compares using character order). I just did a quick test and on my machine it's returning the difference of the last-compared characters. So:

strncmp(s, t, 5) = -100  // '\0' - 'd'
strncmp(s, u, 4) = -7    // 'a' - 'h'

Is that what you're looking for?

Carl Norum
Kind of, yes. I'm more interested in not just knowing the answer but understanding WHY strncmp(s, t, 5) = -100 or why strncmp(s, u, 4) = -7.Knowing the answer is good, but there is the question of WHY that's the answer.
Koby
I wrote that in the answer - that's what the comments are saying. 'a' - 'h' = -7, so the function returns -7.
Carl Norum
Likewise, the null character ('\0') minus 'd' is -100, so that's where that answer comes from.
Carl Norum
I had to look at your answer and the further explanation you provided here in the comments, but I understand now.strncmp takes the first place where the 2 characters aren't equal and returns the difference of those two characters.Thank you.
Koby
But don't forget that there is no guarantee that the returned value from a particular C implementation will be the difference between the characters. The standard just says that it will be "an integer greater than, equal to, or less than zero". (Quoted from my old X3.159-1989.)
Thomas Padron-McCarthy
Thomas is right on the money - all the spec guarantees is the sign of the return value, not its magnitude.
Carl Norum
+1  A: 

it is lexicographic order, strings are compared in alphabetical order from left to right.

So abc < abcdefghijk < hello

strncmp(s, t, 5) = -1 strncmp(s, t, 5) = -1

Kerby82
I think it's semantically correct to return -1, but that's not what my local implementation does. (Mac OS X 10.6.1)
Carl Norum
The requirement is '< 0'; both '-100' and '-1' are less than zero, so both are correct. The typical trick is to return 's1[i] - s2[i]' at the point where the difference is detected.
Jonathan Leffler
@Jonathan is explaining how my local implementation works, seems like it would be the fast way to write it.
Carl Norum
+2  A: 

The characters in the first non-matching positions are cast to unsigned char and then compared numerically - if that character in s1 is less than the corresponding character in s2, then a negative number is returned; if it's greater, a positive number is returned.

caf