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.