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
#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 positive3. 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
#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
std::string equal: 1
strcmp result: 0
C-strings are equal6. Key Takeaways
std::stringsupports==,<,>etc. directly, comparing character contents lexicographically.- C-style strings must be compared with
strcmp(), since==on char arrays compares addresses, not content. strcmp()returns0for equal strings, and negative/positive values otherwise — it is NOT a boolean.- Always test
strcmp(a, b) == 0explicitly for equality to avoid the common inverted-logic bug.
Practice what you learned
1. What does `strcmp("apple", "apple")` return?
2. What is wrong with `if (strcmp(a, b)) { // treat as equal }`?
3. How should two `std::string` objects be checked for equality?
4. Why can't `==` be used directly to compare the contents of two C-style strings (char arrays)?
5. If `strcmp("apple", "banana")` is called, what kind of value is returned?
Was this page helpful?
You May Also Like
C-Style Strings in C++
Understand C-style strings as null-terminated char arrays, the standard library functions used to manipulate them, and why they are riskier than std::string.
String Functions in C++
Explore the most commonly used std::string member functions — length, substr, find, append, at, empty, and c_str — with practical examples.
Relational Operators in C++
Understand C++ relational (comparison) operators such as ==, !=, <, >, <=, and >=, and how they always evaluate to a bool result.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics