C++ strcmp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cstring>

Definition

In C++, strcmp() compares two C-strings, one character at a time. It tells you whether they are the same, or which one comes first. Letter case matters: "Cat" and "cat" are not the same.

Both strings are required. The result is 0 when they match, less than 0 when the first comes first, and greater than 0 when the first comes after. Do not use == on two char* values. That compares addresses, not the letters. Include <cstring>.

Syntax

Compare the two strings, then test the number that comes back.

C++
strcmp("cat", "car");
CallWhat it does
strcmp("cat", "car")Compares cat with car. The result is not 0, because the words differ.
Library declarations
C++
int strcmp(const char* lhs, const char* rhs);

Parameters

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

Return Value

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

C++ Examples

Example 1 — Test Equality

Check whether two C-strings match.

C++
#include <iostream>
#include <cstring>

int main() {
    const char* a = "C++";
    const char* b = "C++";
    const char* c = "Java";

    if (strcmp(a, b) == 0) {
        std::cout << "a and b are equal\n";
    }

    if (strcmp(a, c) != 0) {
        std::cout << "a and c are different\n";
    }
    return 0;
}

How It Works

  1. strcmp(a, b) == 0 means every character matches.
  2. != 0 means the strings differ.
  3. Case and spelling both matter.

Example 2 — Which Comes First?

Use the sign of the result for dictionary order.

C++
#include <iostream>
#include <cstring>

int main() {
    const char* left  = "Apple";
    const char* right = "Banana";

    int result = strcmp(left, right);

    if (result < 0) {
        std::cout << left << " comes before " << right << '\n';
    } else if (result > 0) {
        std::cout << left << " comes after " << right << '\n';
    } else {
        std::cout << "Equal\n";
    }
    return 0;
}

How It Works

  1. Characters are compared until they differ.
  2. 'A' is less than 'B', so result is negative.
  3. Useful for sorting C-string lists.

Example 3 — Clearer Style with std::string

Same equality check without strcmp.

C++
#include <iostream>
#include <string>

int main() {
    std::string a = "C++";
    std::string b = "C++";

    if (a == b) {
        std::cout << "Equal\n";
    }
    return 0;
}

How It Works

  1. std::string overloads == for text content.
  2. No need to remember return codes for a simple match.
  3. Learn strcmp for C APIs; prefer == in new C++.

Frequently Asked Questions

strcmp() compares two C-strings and tells you if they are the same or which one comes first.
Two C-strings. The first is compared with the second.
0 when the strings are the same, a negative number when the first comes first, and a positive number when the first comes after.
Check that strcmp(a, b) == 0. Do not use a == b, because that compares pointers.
Yes. Uppercase and lowercase letters are different.

Next: strcoll()

Compare C-strings using the current locale’s rules.

strcoll() 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.

8 people found this page helpful