C strcmp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

Definition

In C, strcmp() compares two C-strings character by character. Letter case matters, so "Cat" and "cat" are different.

You pass both strings. Both are required. The result is 0 when they match, less than 0 when the first comes first, or greater than 0 when the first comes after. Include <string.h>.

Syntax

Pass both strings. Start with this call.

C
strcmp("apple", "apricot");
CallWhat it does
strcmp("apple", "apricot")Compares the two words from left to right.

Include <string.h>. Do not use == on two char* values. That compares addresses, not text.

Library declarations
C
int strcmp(const char *s1, const char *s2);

Parameters

ParameterDescription
firstRequired. The first C-string.
secondRequired. The second C-string.

Return Value

Return ValueDescription
0The two strings are equal.
Less than 0The first string comes before the second.
Greater than 0The first string comes after the second.

C Examples

Example 1 — Check If Two Strings Match

Never compare C-strings with == on the pointers. Use strcmp and test for zero.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *a = "cat";
    const char *b = "cat";

    if (strcmp(a, b) == 0) {
        printf("equal\n");
    } else {
        printf("different\n");
    }

    return 0;
}

How It Works

  1. Compare contents. strcmp walks both strings byte by byte.
  2. Zero means match. Every character and both terminators lined up.
  3. Branch on == 0. That is the portable equality test.
  4. Pointers can differ. Even if a and b point to different places, the text can still match.

Example 2 — Decide Which String Comes First

Use the sign of the result for sorting and dictionary order (byte values, not locale rules).

C
#include <stdio.h>
#include <string.h>

int main(void) {
    int r = strcmp("apple", "banana");

    if (r < 0) {
        printf("apple comes first\n");
    } else if (r > 0) {
        printf("banana comes first\n");
    } else {
        printf("same\n");
    }

    return 0;
}

How It Works

  1. First bytes differ. 'a' vs 'b' decides the order immediately.
  2. Negative means first. r < 0 means "apple" sorts before "banana".
  3. Ignore the exact number. Do not require -1 or 1.
  4. Locale needs strcoll. For language-aware order, prefer strcoll.

Example 3 — Case Sensitivity

Uppercase and lowercase letters are different bytes, so "Hello" and "hello" are not equal.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    if (strcmp("Hello", "hello") == 0) {
        printf("equal\n");
    } else {
        printf("different (case matters)\n");
    }

    return 0;
}

How It Works

  1. First letters differ. 'H' (72) is not 'h' (104).
  2. Result is non-zero. The strings are treated as different.
  3. Exact tokens need this. Passwords, keywords, and file modes usually care about case.
  4. Fold case separately. Use strcasecmp when users may type either case.

Frequently Asked Questions

strcmp() compares two C-strings. It returns 0 when they match, or a signed value that tells which comes first.
Both are required. first and second are the C-strings to compare.
0 means equal. A negative value means the first comes first. A positive value means the first comes after.
Include <string.h>.
No. == compares addresses. Use strcmp() to compare the characters.

Next: strncmp()

Compare strings with a maximum byte count for safer prefixes.

strncmp() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful