jacob navia said:
There are people that quote the standard at each sentence
but do not actually read it.
I have read the parts of the Standard that relate to the point I am making.
H. is arguing that strncmp is not a string comparison function.
That's right. It *can* compare strings, but then so can memcmp, and I don't
hear anyone arguing that that's a string function.
Can you imagine such nonsense?
As is the case whenever you accuse me of writing nonsense, on reflection it
turns out not to be nonsense. (Would that the reverse were true!) Let's
look at the facts, shall we? Here is the definition of strncmp:
4.11.4.4 The strncmp function
Synopsis
#include <string.h>
int strncmp(const char *s1, const char *s2, size_t n);
Description
The strncmp function compares not more than n characters
(characters that follow a null character are not compared) from the
array pointed to by s1 to the array pointed to by s2 .
Returns
The strncmp function returns an integer greater than, equal to, or
less than zero, according as the possibly null-terminated array
pointed to by s1 is greater than, equal to, or less than the possibly
null-terminated array pointed to by s2 .
That's from C89. The C99 text is identical except that it has "accordingly"
rather than "according". As you can see, the Standard does not insist that
you pass strings to strncmp. The following code is strictly conforming:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char foo[4] = "abcd";
char bar[4] = "abcd";
return strncmp(foo, bar, 4) && EXIT_FAILURE;
}
There are no strings in this program. Neither foo nor bar is a string. And
yet strncmp is required to deal with them correctly. In this case, the
program must return 0. If we were to hack either foo or bar to contain
some different sequence of four characters, the program must return
EXIT_FAILURE. Had we tried the same trick with strcmp (which *is* a string
function), the behaviour would have been undefined.
Yes, you can pass strings to strncmp. You can pass strings to memcmp, too,
but that doesn't make memcmp a string function. You can pass strings to
printf, but that doesn't make printf a string function.
Paragraph 7.21 of the standard is named
"String handling <string.h>"
And yet it contains prototypes for mem*, which are not string handling
functions. So the mere appearance of a function declaration in string.h is
not sufficient to qualify it as a string handling function, as I have
already explained, but which you unaccountably appear to have missed.
In that section we have the subsection
<quote>
7.21.4: Comparison functions
The sign of a nonzero value returned by the comparison functions memcmp,
strcmp, and strncmp is determined by the sign of the difference between
the values of the first pair of characters (both interpreted as unsigned
char) that differ in the objects being compared
<end quote>
You read correctly Mr H?
Yes. Nothing in the above paragraph mentions strings. Furthermore, in that
paragraph strncmp is placed in the same category as memcmp, which is not a
string handling function.
This really is very simple.
"comparison functions ... strncmp" !!!
Yes, I agree that it's a comparison function. So what?