Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

strcmp - Compare two strings

&pagelevel(4)&pagelevel

Definition

#include <string.h>

int strcmp(const char *s1, const char *s2);

strcmp compares strings s1 and s2 lexically, e.g.:

"circle" is lexically less than "circular",
"bustle" is lexically greater than "bus".

Return val.

< 0

= 0

> 0

s1 is lexically less than s2.

s1 and s2 are lexically equal.

s1 is lexically greater than s2.

Note

Strings terminated with the null byte (\0) are expected as arguments.

Example

The following program searches the name list list for an input name:

#include <stdio.h>
#include <string.h>
char *list[] = {"anne", "peter", "walter", "john" };
int main(int argc, char *argv[])
{
  int j, i = 0;
  while((i <= 3) && (j = strcmp(argv[1], list[i++])));
  if (j == 0)
     printf("The candidate is already known!\n");
  else
     printf("This is a new candidate!\n");
     return 0;
}

See also

strncmp