Compare strings.
#include <string.h> | Required only for function declarations |
int strcmp( const char *string1, const char *string2 );
int __far _fstrcmp( const char __far *string1, const char __far *string2 );
string1 | String to compare | |
string2 | String to compare |
The strcmp and _fstrcmp functions compare string1 and string2 lexicographically and return a value indicating their relationship, as follows:
Value | Meaning |
< 0 | string1 less than string2 |
= 0 | string1 identical to string2 |
> 0 | string1 greater than string2 |
The strcmp and _fstrcmp functions operate on null-terminated strings. The string arguments to these functions are expected to contain a null character ('\0') marking the end of the string.
The _fstrcmp function is a model-independent (large-model) form of the strcmp function. The behavior and return value of _fstrcmp are identical to those of the model-dependent function strcmp, with the exception that the arguments are far pointers.
Both the _stricmp function (described later in this book) and the _strcmpi function compare strings by first converting them to their lowercase forms.
Note that two strings containing characters located between 'Z' and 'a' in the ASCII table ('[', '\', ']', '^', '_', and '`') compare differently depending on their case. For example, the two strings, "ABCDE" and "ABCD^", compare one way if the comparison is lowercase ("abcde" > "abcd^") and compare the other way ("ABCDE" < "ABCD^") if it is uppercase.
The return values for these functions are described above.
strcmp
Standards:ANSI, UNIX
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
_fstrcmp
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:None
memcmp, _memicmp, strncat, strncmp, strncpy, _strnicmp, strrchr, strspn
/* STRCMP.C */
#include <string.h>
#include <stdio.h>
char string1[] = "The quick brown dog jumps over the lazy fox";
char string2[] = "The QUICK brown dog jumps over the lazy fox";
void main( void )
{
char tmp[20];
int result;
/* Case sensitive */
printf( "Compare strings:\n\t%s\n\t%s\n\n", string1, string2 );
result = strcmp string1, string2 );
if( result > 0 )
strcpy( tmp, "greater than" );
else if( result < 0 )
strcpy( tmp, "less than" );
else
strcpy( tmp, "equal to" );
printf( "\tstrcmp: String 1 is %s string 2\n", tmp );
/* Case insensitive (could use equivalent _stricmp) */
result = _stricmp( string1, string2 );
if( result > 0 )
strcpy( tmp, "greater than" );
else if( result < 0 )
strcpy( tmp, "less than" );
else
strcpy( tmp, "equal to" );
printf( "\t_stricmp: String 1 is %s string 2\n", tmp );
}
Compare strings:
The quick brown dog jumps over the lazy fox
The QUICK brown dog jumps over the lazy fox
strcmp: String 1 is greater than string 2
_stricmp: String 1 is equal to string 2