---
name: "strcmp"
section: "3"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/strcmp"
---

# strcmp(3)

strings compare

## NAME

strcmp - strings compare

## LIBRARY

Standard C library (*libc*, *-lc*)

## SYNOPSIS

```
#include <string.h>
```

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

## DESCRIPTION

The **strcmp**() function compares the two strings *s1* and *s2*, as if by calling [**memcmp**(3)](/memcmp/3.md).

It is equivalent to

```
memcmp(s1, s2, MIN(strlen(s1),strlen(s2))+1)
```

## RETURN VALUE

See [**memcmp**(3)](/memcmp/3.md).

## ATTRIBUTES

For an explanation of the terms used in this section, see [**attributes**(7)](/attributes/7.md).

| | | |
| --- | --- | --- |
| **Interface** | **Attribute** | **Value** |
| **strcmp**() | Thread safety | MT-Safe |

## STANDARDS

C11, POSIX.1-2008.

## HISTORY

POSIX.1-2001, C89, SVr4, 4.3BSD.

## CAVEATS

The locale is not taken into account (for a locale-aware comparison, see [**strcoll**(3)](/strcoll/3.md)).

## EXAMPLES

The program below can be used to demonstrate the operation of **strcmp**().

```
$ ./strcmp ABC ABC;
<str1> and <str2> are equal
$ ./strcmp ABC AB;      # 'C' is ASCII 67; 'C' - '\0' = 67
<str1> is greater than <str2> (67)
$ ./strcmp ABA ABZ;     # 'A' is ASCII 65; 'Z' is ASCII 90
<str1> is less than <str2> (-25)
$ ./strcmp ABJ ABC;
<str1> is greater than <str2> (7)
$ ./strcmp $'\201' A;   # 0201 - 0101 = 0100 (or 64 decimal)
<str1> is greater than <str2> (64)
```

The last example uses [**bash**(1)](/bash/1.md)-specific syntax to produce a string containing an 8-bit ASCII code; the result demonstrates that the string comparison uses unsigned characters.

### Program source

```
// strcmp.c
// Licensed under GNU General Public License v2 or later.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char *argv[])
{
    int res;
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <str1> <str2>\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    res = strcmp(argv[1], argv[2]);
    if (res == 0) {
        printf("<str1> and <str2> are equal");
        printf("\n");
    } else if (res < 0) {
        printf("<str1> is less than <str2> (%d)\n", res);
    } else {
        printf("<str1> is greater than <str2> (%d)\n", res);
    }
    exit(EXIT_SUCCESS);
}
```

## SEE ALSO

[**memcmp**(3)](/memcmp/3.md), [**strcasecmp**(3)](/strcasecmp/3.md), [**strcoll**(3)](/strcoll/3.md), [**streq**(3)](/streq/3.md), [**string**(3)](/string/3.md), [**strverscmp**(3)](/strverscmp/3.md), [**wcscmp**(3)](/wcscmp/3.md)
