strncmp() — Compare Strings

Format

#include <string.h>

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

General Description

The strncmp() built-in function compares at most the first count characters of the string pointed to by string1 to the string pointed to by string2.

The string arguments to the function should contain a NULL character (\0) marking the end of the string.

The relation between the strings is determined by the sign of the difference between the values of the leftmost first pair of characters that differ. The values depend on character encoding. This function is not locale sensitive.

Returned Value

The strncmp() function returns a value indicating the relationship between the substrings, as follows:
Value
Meaning
< 0
String pointed to by substring1 less than string pointed to by substring2
= 0
String pointed to by substring1 equivalent to string pointed to by substring2
> 0
String pointed to by substring1 greater than string pointed to by substring2

Related Information