C++ strcmp()

The strcmp() function in C++ compares two null-terminating strings (C-strings). The comparison is done lexicographically. It is defined in the cstring header file.

Example

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

int main() {
  char str1[] = "Megadeth";
  char str2[] = "Metallica";

// compare str1 and str2 lexicographically int result = strcmp(str1, str2);
cout << result; return 0; } // Output: -1

strcmp() Syntax

The syntax of strcmp() is:

strcmp(const char* lhs, const char* rhs);

Here,

  • lhs stands for left hand side
  • rhs stands for right hand side

strcmp() Parameters

The strcmp() function takes the following parameters:

  • lhs - pointer to the C-string that needs to be compared
  • rhs - pointer to the C-string that needs to be compared

strcmp() Return Value

The strcmp() function returns:

  • a positive value if the first differing character in lhs is greater than the corresponding character in rhs.
  • a negative value if the first differing character in lhs is less than the corresponding character in rhs.
  • 0 if lhs and rhs are equal.

strcmp() Prototype

The prototype of strcmp() as defined in the cstring header file is:

int strcmp( const char* lhs, const char* rhs );
  • The strcmp() compares the contents of lhs and rhs lexicographically.
  • The sign of the result is the sign of difference between the first pairs of characters that differ in lhs and rhs.

strcmp() Undefined Behavior

The behaviour of strcmp() is undefined if:

  • either of lhs or rhs do not point to C-strings (null-terminated strings)

Example 1: C++ strcmp()

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

int main() {
  char str1[] = "Megadeth";
  char str2[] = "Metallica";

// returns -1 because "Megadeth" < "Metallica" lexicographically int result = strcmp(str1, str2);
cout << "Comparing " << str1 << " and " << str2 << ": " << result << endl;
// returns 1 because "Metallica" > "Megadeth" lexicographically result = strcmp(str2, str1);
cout << "Comparing " << str2 << " and " << str1 << ": " << result << endl;
// returns 1 because "Megadeth" = "Megadeth" lexicographically result = strcmp(str1, str1);
cout << "Comparing " << str1 << " and " << str1 << ": " << result; return 0; }

Output

Comparing Megadeth and Metallica: -1
Comparing Metallica and Megadeth: 1
Comparing Megadeth and Megadeth: 0

Example 2: Use strcmp() in a User-Defined Function

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

// function to display the result of strcmp()
void display(char *lhs, char *rhs) {

// compare display() parameters lhs and rhs int result = strcmp(lhs, rhs);
if (result > 0) cout << rhs << " precedes " << lhs << endl; else if (result < 0) cout << rhs << " follows " << lhs << endl; else cout << lhs << " and " << rhs << " are same" << endl; } int main() { char str1[] = "Armstrong"; char str2[] = "Army"; // lhs = str1, rhs = str2 display(str1, str2); // lhs = str2, rhs = str1 display(str2, str1); // lhs = str1, rhs = str1 display(str1, str1); // lhs = str2, rhs = str2 display(str2, str2); return 0; }

Output

Army follows Armstrong
Armstrong precedes Army
Armstrong and Armstrong are same
Army and Army are same

Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges