strcmp -- Compare Two Null-Terminated Strings


SYNOPSIS
#include <string.h>
int strcmp(const char *str1, const char *str2);
DESCRIPTION
strcmp
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.
RETURN VALUE
The return value from strcmp
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
. No other assumptions
should be made about the value returned by strcmp
.
CAUTION
If one of the arguments of strcmp
is not properly terminated, a
protection or addressing exception may occur. If one of the arguments to the
built-in version of strcmp
is a constant, the compiler generates a CLC
instruction to perform the entire comparison. If the variable argument is not
null terminated, the character-by-character comparison may perform as expected,
but a comparison by the CLC instruction may cause an addressing exception in
rare cases.
IMPLEMENTATION
The compiler generates inline code for strcmp
unless strcmp
is
undefined (by an #undef
statement) to prevent this. The inline
code may still call a library routine in special cases.
EXAMPLE
#include <lcstring.h>
#include <stdio.h>
#include <stdlib.h>
main()
{
char command[20];
int n = 0;
for(;;) {
++n;
printf("Enter command # %dn", n);
puts("Enter quit to terminate/any other command to continue.");
gets(command);
if (strcmp(command, "quit") == 0) break;
/* Determine whether command is equal to quit. */
strlwr(command);
if (strcmp(command, "quit") == 0)
exit(0);
puts("Did you mean to say quit? (Case is significant.)");
}
}
RELATED FUNCTIONS
memcmp
, strcoll
, strncmp
, strxfrm
SEE ALSO
String Utility Functions