Difference between strcmp, strncmp and memcmp

https://blog.csdn.net/ttgoo/article/details/5828978

函数:int memcmp (const void *a1, const void *a2, size_t size) 

       The function memcmp is used to compare the first size characters of the strings s1 and s2. 
      If the two character blocks are the same, memcmp will return 0.

Function: int strcmp (const char *s1, const char *s2) 
       This function is used to compare the s1 and s2 strings. This function will return a value whose sign is related to the result of the comparison of the first pair of different characters. 
      strcmp will return 0 if the two strings are equal. 
       If s1 is a substring of s2, s1 is less than s2. 
In addition, there is the function 
    int strncmp (const char *s1, const char *s2, size_t size), 
    which is very similar to strcmp. The difference is that the strncmp function is specified to compare size characters. That is, if the first size characters of the strings s1 and s2 are the same, the function returns a value of 0.

Feature comparison:

     Both can be used for string comparison, but there is a big difference between the two, because strcmp is compared byte-wise, and the comparison process will check whether "/0" appears Terminator, once the terminator is encountered during the advancement of any string pointer, the comparison will be terminated. The memcmp function is used to compare whether the contents of two memory blocks are equal. When used for string comparison, it is usually used to test whether the strings are equal, and byte-wise string comparison is not often performed. If the objects to be compared contain some "holes" caused by spaces filled in struct objects due to boundary alignment requirements, extra spaces at the end of unions, and unused parts of the space allocated by strings, it is best to Use memcmp to do it. The content of these "holes" is indeterminate, and the result is ambiguous when performing byte-wise comparisons.

Efficiency difference: 
     strcmp compares strings, while memcmp compares memory blocks. strcmp needs to always check whether the /0 character at the end of the string is encountered, while memcmp does not have to worry about this problem at all, so the efficiency of memcmp is higher than that of strcmp

Example of use:

Given a structure definition as follows: 
struct foo 

    unsigned char tag; 
    union 
       { 
         double f; 
        long i; 
          char *p; 
       } value; 
}; 
      If you want to compare two struct foo objects, it is recommended to use memcmp. 
     Given an example of string comparison, determine whether the first four characters in the string str are 0x80100001, because 0x00 is a terminator for strings, if strncmp is used, then strncmp(str,"/x80/x10 /x00 /x01",4), the actual effect is to only judge whether it contains 0x8010, that is to say, once the first two characters in str are 0x8010, it will return 0, indicating the same, obviously this is incorrect! At this point, memcmp(str,"/x80/x10/x00/x01",4) should be used, so that the purpose is achieved

Attached: Linux source code of strcmp, strncmp, memcmp

/** 
* strcmp - Compare two strings 
* @cs: One string 
* @ct: Another string 
*/ 
int strcmp(const char *cs, const char *ct) 

        signed char __res;

        while (1) { 
                if ((__res = *cs - *ct++) != 0 || !*cs++)

//When the end character /0 is compared, __res = *cs - *ct has been done, but the values ​​of *cs and *ct are both /0, so the return must be 0 
                        break; 
        } 
        return __res; 

/** 
* strncmp - Compare two length-limited strings 
* @cs: One string 
* @ct: Another string 
* @count: The maximum number of bytes to compare 
*/ 
int strncmp(const char *cs, const char *ct , size_t count) 

        signed char __res = 0;

        while (count) { 
                if ((__res = *cs - *ct++) != 0 || !*cs++) //When the end character /0 is compared, __res = *cs - *ct has been done, so When the length is not equal, the return must not be 0 
                        break; 
                count--; 
        } 
        return __res; 

/** 
* memcmp - Compare two areas of memory 
* @cs: One area of ​​memory 
* @ct: Another area of ​​memory 
* @ count: The size of the area. 
*/ 
int memcmp(const void *cs, const void *ct, size_t count) 

        const unsigned char *su1, *su2; 
        int res = 0; 
        for (su1 = cs, su2 = ct ; 0 < count; ++su1, ++su2, count--) 
                if ((res = *su1 - *su2) != 0) 
                        break; 
        return res; 
}

test program:

#include 
#include 
#include

using namespace std;

int main() 

string str1 = "abcd"; 
string str2 = "abcdef";

int result1 = 0; 
int result2 = 0; 
int result3 = 0;

result1 = strcmp(str1.c_str(),str2.c_str()); 
result2 = strncmp(str1.c_str(),str2.c_str(),7); 
result3 = memcmp(str1.c_str(),str2.c_str(),7);

cout<<"strcmp: "<cout<<"strncmp: "<cout<<"memcmp: "<

getchar(); 
return 0; 
}

get conclusion:

Example string str1 = "abcd";

           srr2 = "abcdef";

(1) The comparison result of strmcp is non-zero (unequal).

(2) strncmp, if count <= 4, the result is 0 (equal); if count > 4, the result is -101 (because the length of the shortest string is 4, so the comparison ends at the 5th character, and the value is ' /0' ascii code minus the ascii value of 'e').

(3) memcpy, if count <= 4, the result is 0 (equal); if count > 4, the result is -1 (the count byte ends when the comparison is reached).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325726524&siteId=291194637