strncmp -- Compare Portions of Two Strings


SYNOPSIS
#include <string.h>
int strncmp(const char *str1, const char *str2,
size_t maxlen);
DESCRIPTION
strncmp
compares two character strings (str1
and str2
)
using the standard EBCDIC collating sequence. The return value has the
same relationship to 0 as str1
has to str2
. If two strings
are equal up to the point at which one terminates (that is, contains a
null character), the longer string is considered greater. If maxlen
characters are inspected from each string and no inequality is
detected, the strings are considered equal.
RETURN VALUE
The return value from strncmp
is 0 if the two strings are equal,
less than 0 if str1
compares less than str2
, and greater than
0 if str1
compares greater than str2
(within the first
maxlen
characters). No other assumptions should be made about the
value returned by strncmp
.
CAUTION
If the maxlen
value is specified as 0, a result of 0 is returned.
If the value is a negative integer, it is interpreted as a very large
unsigned
value. This may cause a protection or addressing
exception, but this is unlikely because comparison ceases as soon as
unequal characters are found.
IMPLEMENTATION
strncmp
is implemented as a built-in function, unless you use it with
undef
.
EXAMPLE
Compare this example to the example for strcmp
:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
main()
{
char command[20];
int n = 0;
do{
n++;
printf("You have executed this loop %d times.n", n);
puts("Enter quit(may be abbreviated) to end program,");
puts(" or any other command to continue.");
gets(command);
}
while(strncmp(command, "quit", strlen(command)) != 0);
exit(0);
}
RELATED FUNCTIONS
memcmp
, strcmp
SEE ALSO
String Utility Functions