Previous Page
Next Page

strncmp

Compares the first n characters of two strings

#include <string.h>
int strncmp ( const char *s1 , const char *s2 , size_t n  );

The strncmp( ) function compares at most the first n characters in the two strings addressed by its pointer arguments. Characters that follow a null character are ignored. strncmp( ) returns a value indicating the result as follows:


Zero

The two strings, or arrays of n characters, are equal.


Greater than zero

The string or array of n characters addressed by s1 is greater than that addressed by s2.


Less than zero

The string or array of n characters addressed by s1 is less than that addressed by s2.

Example

char *weekdays[ ] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
                     "Friday", "Saturday" };
char date[ ] = "Thu, 10 Mar 2005 13:44:18 +0100";
int dow;
for ( dow = 0; dow < 7; dow++ )
  if ( strncmp( date, weekdays[dow], 3 ) == 0 )
    break;

See Also

strcmp( ), wcsncmp( ), wcscmp( )


Previous Page
Next Page