The difference between strcmp, strncmp and memcmp

Source: Internet
Author: User
Tags strcmp

function: int memcmp (const void *A1, const void *A2, size_t size)
function memcmp is used to compare the first-size characters of string S1 with S2.
If the character blocks are the same, MEMCMP returns 0.

function: int strcmp (const char *S1, const char *S2)
This function is used to compare S1 and S2 strings, which returns a value whose symbols are related to the comparison of the first pair of characters.
If two strings are equal, strcmp will return 0.
If S1 is a substring of S2, S1 is less than S2
In addition, there are functions
int strncmp (const char *S1, const char *S2, size_t size)
This function is very similar to strcmp. The difference is that the STRNCMP function specifies the size character of the comparison. In other words, if the string S1 is the same as the first size character of the S2, the function returns a value of 0.

function comparison:

Both can be used for comparison of strings, but the two are quite different because the strcmp is compared by byte (Byte-wise), and the comparison checks to see if the "/0" Terminator is present, and the comparison is terminated once any string pointer advances and a terminator is encountered. The MEMCMP function, which is used to compare the contents of two memory blocks, is often used for string comparisons to test whether strings are equal or not, and does not often perform a byte-wise string comparison. It is best to use memcmp if the object to be compared contains some space that is filled in the structure object due to the boundary alignment requirements, additional spaces for Union (union) ending, and that is caused by the unused portion of the space allocated by the string. The contents of these "holes" are uncertain and the results are ambiguous when performing byte-wise comparisons.

Efficiency variance:
strcmp Comparison of strings, and memcmp compared to the memory block, strcmp need to check whether the end of the string is encountered/0 characters, and memcmp do not have to worry about this problem, so memcmp more efficient than strcmp

Use examples:

A structure definition is given as follows:
struct FOO
{
unsigned char tag;
Union
{
Double F;
Long I;
Char *p;
} value;
};
It is advisable to use memcmp if you want to compare two struct Foo objects.
Gives an example of a string comparison to determine if the first four characters in string str are 0x80100001 because 0x00 is a terminator for a string, if you use strncmp strncmp (str, "/x80/x10/x00/x01", 4) , the actual effect is only to determine whether the 0x8010 is included, which means that once the first two characters in Str are 0x8010, the return of 0 indicates the same, which is obviously incorrect. MEMCMP (str, "/x80/x10/x00/x01", 4) should be used at this point so that it achieves its purpose

Attached: strcmp,strncmp,memcmp Linux Source code

/**
* 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++)

Compared to Terminator/0, the __res = *cs-*ct has been done, but *cs and *CT values are all/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++)//Compare to Terminator/0, when, has done __res = *cs-*ct, so unequal length, definitely return not to 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;
}

Come to the conclusion that:

Example String str1 = "ABCD";

SRR2 = "abcdef";

(1) The result of STRMCP comparison is not 0 (unequal).

(2) strncmp, if Count <= 4, the result is 0 (equal); if Count > 4, the result is 101 (because the shortest string length is 4, the comparison to the 5th character is ending, and the value is '/0 ' ASCII minus the ASCII value of ' e ').

(3) memcpy, if Count <= 4, the result is 0 (equal); if Count > 4, the result is-1 (the comparison to count bytes ends).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.