Koby
Koby

Reputation: 7367

strncmp C Exercise

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.

Upvotes: 0

Views: 6889

Answers (4)

Nicholas Carey
Nicholas Carey

Reputation: 74297

The contract for strncmp is to return an integral value whose sign indicates the result of the comparison:

  • a negative value indicates that the 1st operand compares as being "less than" the 2nd operand,
  • a positive, non-zero value indicates that the 1st operand compares as being "greater than" than the 2nd operand, and
  • 0 indicates that the two operands compare as being "equal to" each other.

The reason it's defined that way, rather than, say, "return -1 for "less than", 0 for "equal to" and +1 for "greater than" is to not constrain the implementation.

The value returned for a particular C runtime library is dependent upon how the function is implemented. The Posix specification (IEEE 1003.1) for strncmp() (which tracks the C Standard) says:

The strncmp() function shall compare not more than n bytes (bytes that follow a null byte are not compared) from the array pointed to by s1 to the array pointed to by s2.

The sign of a non-zero return value is determined by the sign of the difference between the values of the first pair of bytes (both interpreted as type unsigned char) that differ in the strings being compared.

That should be about all you need to know to implement it. You should note, though that:

  • strncmp() is not "safe", in the sense that it is subject to buffer overflows. A proper implementation will merrily compare characters until it encounters an ASCII NUL, hits the maximum length, or tries to access protected memory.
  • The specification says that the sign of the return value is based on the delta between the 1st pair of characters that differ; no particular return value is mandated.

Good luck.

Upvotes: 2

caf
caf

Reputation: 239111

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.

Upvotes: 2

Carl Norum
Carl Norum

Reputation: 224982

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?

Upvotes: 3

Kerby82
Kerby82

Reputation: 5146

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

Upvotes: 1

Related Questions