strcmp() — Compare Strings

Format

#include <string.h>

int strcmp(const char *string1, const char *string2);

General Description

The strcmp() built-in function compares the string pointed to by string1 to the string pointed to by string2 The string arguments to the function must contain a NULL character (\0) marking the end of the string.

The relation between the strings is determined by subtracting: string1[i] - string2[i], as i increases from 0 to strlen of the smaller string. The sign of a nonzero return value is determined by the sign of the difference between the values of the first pair of bytes (both interpreted as type unsigned char) that differ in the strings being compared. This function is not locale-sensitive.

Returned Value

strcmp() returns a value indicating the relationship between the strings, as listed below.
Value
Meaning
< 0
String pointed to by string1 less than string pointed to by string2
= 0
String pointed to by string1 equivalent to string pointed to by string2
> 0
String pointed to by string1 greater than string pointed to by string2

Related Information