strcmp in C & C++

Let's first understand what is C-string and std::string, so click here to learn.

strcmp would check if the passed strings are equal, or <, or > and returns either 0, or  less than 0, or greater than 0. strncmp is used when strings are not null terminated. Hence, strcmp has 3 way-output. 

strcmp vs overloaded operators(==, <, >) in C

strcmp compares the actual C-string content, while using == between two C-string is asking if these two char pointers point to the same position.

If we have some C-string defined as following:

char string_a[] = "foo";
char string_b[] = "foo";

char * string_c = string_a;

strcmp(string_a, string_b) == 0 would return true, while string_a == string_b 
would return false. Only when "comparing" string_a and string_c using == would return true.
If you want to compare the actual contents of two C-string but not whether they are just alias
of each other, use strcmp.

strcmp vs overloaded operators(==, <, >) in C++

If you are using C++ instead of C as, then you should use std::string. For example,

std::string string_d = "bar";
std::string string_e = "bar";

overloaded operators(==, <, >) 

string_d == string_e  would return true

compare 

string_d.compare(string_e) would return 0, which is the C++ version of strcmp. So, this returns 3 way output similar to strcmp. compare() will compare up to the first null character.

strcmp (Not recommended)

In C++, strcmp still works but the arguments should be string_d.c_str() & string_e.c_str().


Note: For C++, go for == as it has binary output and simple. 


#include <iostream>
#include <cstring>
#include <string>

int main()
{
    const char string_a[4] = "foo";
    const char string_b[4] = "foo";
    const char * string_c = string_a;
   
    std::cout << std::boolalpha; // for better bool output
    
    std::cout << "C way for char array (==):\nstring_a == string_b: " << 
                 (string_a == string_b) << std::endl;
    std::cout << "C way: string_a == string_c: " << 
                  (string_a == string_c) << std::endl;
    std::cout << std::endl;
    std::cout << "C way for char array strcmp:\nstrcmp(string_a, string_b): " << 
                 (std::strcmp(string_a, string_b) == 0) << std::endl;
    std::cout << "strcmp(string_a, string_c): " << 
                 (std::strcmp(string_a, string_c) == 0) << std::endl;
    
    std::cout << "\n";
    
    std::string string_d = "bar";
    std::string string_e = "bar";
    std::string string_f = "dar";
    std::string string_g = "bar\0hello";
    std::string string_h = "ba\0hello";
    
    std::cout << "The C++ way for string - overloaded operator:\nstring_d == string_e: " << 
                 (string_d == string_e) << std::endl;
    std::cout << "\nThe C++ way for string - strcmp:\nstrcmp(string_d, string_e): " << 
                 (std::strcmp(string_d.c_str(), string_e.c_str()) == 0) << std::endl;
                 
    std::cout << "\nThe C++ way for string - compare:\nstring_d.compare(string_e): " << 
                 (string_d.compare(string_e) == 0) << std::endl;
    std::cout << "string_d.compare(string_f): " << 
                 (string_d.compare(string_f) < 0) << std::endl;
    std::cout << "string_d.compare(string_g): " << 
                 (string_d.compare(string_g) == 0) << std::endl;
    std::cout << "string_d.compare(string_h): " << 
                 (string_d.compare(string_h) == 0);
    return 0;
}


Output:

C way for char array (==):
string_a == string_b: false
C way: string_a == string_c: true

C way for char array strcmp:
strcmp(string_a, string_b): true
strcmp(string_a, string_c): true

The C++ way for string - overloaded operator:
string_d == string_e: true

The C++ way for string - strcmp:
strcmp(string_d, string_e): true

The C++ way for string - compare:
string_d.compare(string_e): true
string_d.compare(string_f): true
string_d.compare(string_g): true
string_d.compare(string_h): false


The code is available here


strcmp Common Mistakes in C

When the array(p_v4 or p_v6) is memset, all the array values will be initialized to 0 via memset or array initialization.

So, the following condition always gets passed, so don't check like this. 

(strncmp(p_v4, "0", 1) != 0) && (strncmp(p_v6, "0", 1) != 0)                



Reference:




Comments