100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

String Comparison in C++

Learn how to compare strings in C++ using relational operators for std::string and strcmp() for C-style strings, and avoid the strcmp() boolean trap.

StringsBeginner6 min readJul 7, 2026
Analogies

1. Intro

Comparing strings is a common task — checking if two strings are equal, or determining which one comes first alphabetically. std::string supports this directly using familiar relational operators like ==, <, and >, which compare characters lexicographically (dictionary order based on character codes). C-style strings, on the other hand, must be compared using the strcmp() function from <cstring>, since == on C-style strings compares pointer addresses, not content.

🏏

Cricket analogy: Checking if two player names match with == on std::string works directly like comparing scorecards letter by letter, but comparing C-style name arrays with == would wrongly compare their memory slots, not whether "Kohli" actually equals "Kohli", so strcmp() must be used instead.

2. Syntax

cpp
#include <string>
#include <cstring>
using namespace std;

// std::string comparison
string a = "apple";
string b = "banana";
bool eq = (a == b);      // false
bool less = (a < b);      // true, 'a' < 'b' alphabetically

// C-style string comparison
char c1[] = "apple";
char c2[] = "banana";
int result = strcmp(c1, c2);   // negative, zero, or positive

3. Explanation

For std::string, ==, !=, <, <=, >, and >= all work directly and compare the strings character-by-character in lexicographic order, similar to how words are ordered in a dictionary. For C-style strings, you must use strcmp(str1, str2), which returns 0 if the strings are equal, a negative value if str1 comes before str2 lexicographically, and a positive value if str1 comes after str2. Using == directly on two char* or char[] variables compares their memory addresses, not their contents, which almost never gives the intended result.

🏏

Cricket analogy: Sorting batting-order names alphabetically works directly with < on std::string, dictionary-style; but strcmp("Dhoni", "Kohli") returns a negative number since Dhoni comes first, and comparing two char* player-name buffers with == would compare their addresses, almost never giving the intended name match.

strcmp() does NOT return a boolean. It returns 0 when the strings are equal — which is falsy in an if condition! Writing if (strcmp(a, b)) to mean "if equal" is a very common beginner mistake because it actually means "if NOT equal". Always compare explicitly: if (strcmp(a, b) == 0) for equality.

4. Example

cpp
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    string a = "apple";
    string b = "apple";
    cout << "std::string equal: " << (a == b) << endl;

    char c1[] = "apple";
    char c2[] = "apple";
    int result = strcmp(c1, c2);
    cout << "strcmp result: " << result << endl;
    cout << (result == 0 ? "C-strings are equal" : "C-strings differ") << endl;
    return 0;
}

5. Output

text
std::string equal: 1
strcmp result: 0
C-strings are equal

6. Key Takeaways

  • std::string supports ==, <, > etc. directly, comparing character contents lexicographically.
  • C-style strings must be compared with strcmp(), since == on char arrays compares addresses, not content.
  • strcmp() returns 0 for equal strings, and negative/positive values otherwise — it is NOT a boolean.
  • Always test strcmp(a, b) == 0 explicitly for equality to avoid the common inverted-logic bug.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#StringComparisonInC#String#Comparison#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep