strncmp, _fstrncmp

Description

Compare characters of two strings.

#include <string.h> Required only for function declarations  

int strncmp( const char *string1, const char *string2, size_t count );

int __far _fstrncmp( const char __far *string1, const char __far *string2, size_t count );

string1 String to compare  
string2 String to compare  
count Number of characters compared  

Remarks

The strncmp and _fstrncmp functions lexicographically compare, at most, the first count characters of string1 and string2 and return a value indicating the relationship between the substrings, as listed below:

Value Meaning

< 0 string1 less than string2
= 0 string1 equivalent to string2
> 0 string1 greater than string2

The _strnicmp function is a case-insensitive version of strncmp.

The _fstrncmp function is a model-independent (large-model) form of the strncmp function. The behavior and return value of _fstrncmp are identical to those of the model-dependent function strncmp, with the exception that all the arguments and return values are far.

Return Value

The return values for these functions are described above.

Compatibility

strncmp

Standards:ANSI, UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

_fstrncmp

Standards:None

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:None

See Also

strcat, strcmp, strcpy, strncat, strncpy, strrchr, _strset, strspn

Example

/* STRNCMP.C */

#include <string.h>

#include <stdio.h>

char string1[] = "The quick brown dog jumps over the lazy fox";

char string2[] = "The QUICK brown fox jumps over the lazy dog";

void main( void )

{

char tmp[20];

int result;

printf( "Compare strings:\n\t\t%s\n\t\t%s\n\n", string1, string2 );

printf( "Function:\tstrncmp (first 10 characters only)\n" );

result = strncmp( string1, string2 , 10 );

if( result > 0 )

strcpy( tmp, "greater than" );

else if( result < 0 )

strcpy( tmp, "less than" );

else

strcpy( tmp, "equal to" );

printf( "Result:\t\tString 1 is %s string 2\n\n", tmp );

printf( "Function:\t_strnicmp (first 10 characters only)\n" );

result = _strnicmp( string1, string2, 10 );

if( result > 0 )

strcpy( tmp, "greater than" );

else if( result < 0 )

strcpy( tmp, "less than" );

else

strcpy( tmp, "equal to" );

printf( "Result:\t\tString 1 is %s string 2\n\n", tmp );

}

Output

Compare strings:

The quick brown dog jumps over the lazy fox

The QUICK brown fox jumps over the lazy dog

Function: strncmp (first 10 characters only)

Result: String 1 is greater than string 2

Function: _strnicmp (first 10 characters only)

Result: String 1 is equal to string 2