Name

strncmp

Synopsis

Compares the first n characters of two strings

#include <string.h>
intstrncmp( 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;

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.