Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between memcmp, strcmp and strncmp in C?

I wrote this small piece of code in C to test memcmp() strncmp() strcmp() functions in C.

Here is the code that I wrote:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
        char *word1="apple",*word2="atoms";

        if (strncmp(word1,word2,5)==0)
                printf("strncmp result.\n");
        if (memcmp(word1,word2,5)==0)
                printf("memcmp result.\n");
        if (strcmp(word1,word2)==0)
                printf("strcmp result.\n");
}

Can somebody explain me the differences because I am confused with these three functions?

My main problem is that I have a file in which I tokenize its line of it,the problem is that when I tokenize the word "atoms" in the file I have to stop the process of tokenizing.

I first tried strcmp() but unfortunately when it reached to the point where the word "atoms" were placed in the file it didn't stop and it continued,but when I used either the memcmp() or the strncmp() it stopped and I was happy.

But then I thought,what if there will be a case in which there is one string in which the first 5 letters are a,t,o,m,s and these are being followed by other letters.

Unfortunately,my thoughts were right as I tested it using the above code by initializing word1 to "atomsaaaaa" and word2 to atoms and memcmp() and strncmp() in the if statements returned 0.On the other hand strcmp() it didn't. It seems that I must use strcmp().

like image 751
und3rd06012 Avatar asked Oct 26 '12 22:10

und3rd06012


People also ask

What is the difference between strcmp () and strncmp () functions?

strcmp compares both the strings till null-character of either string comes whereas strncmp compares at most num characters of both strings.

What is the difference between strcmp () and Stricmp () in C?

strcmpi and stricmp are case-insensitive versions of strcmp . They work identically in all other respects. The method strcmpi is the same as stricmp . strcmpi is implemented through a macro in string.

Is strncmp faster than strcmp?

If n is sufficiently large that strncmp will compare the whole strings (so that the behavior becomes effectively the same as strcmp ) then strncmp is likely to be moderately slower because it also has to keep track of a counter, but the difference might or might not be measurable or even present in a given ...

Is strncmp safer than strcmp?

Disadvantage of using strn is extra compare and decrement operation on counter. In few words: strncmp is safer then strcmp, but it is slower too.


3 Answers

In short:

  • strcmp compares null-terminated C strings
  • strncmp compares at most N characters of null-terminated C strings
  • memcmp compares binary byte buffers of N bytes

So, if you have these strings:

const char s1[] = "atoms\0\0\0\0";  // extra null bytes at end const char s2[] = "atoms\0abc";     // embedded null byte const char s3[] = "atomsaaa"; 

Then these results hold true:

strcmp(s1, s2) == 0      // strcmp stops at null terminator strcmp(s1, s3) != 0      // Strings are different strncmp(s1, s3, 5) == 0  // First 5 characters of strings are the same memcmp(s1, s3, 5) == 0   // First 5 bytes are the same strncmp(s1, s2, 8) == 0  // Strings are the same up through the null terminator memcmp(s1, s2, 8) != 0   // First 8 bytes are different 
like image 91
Adam Rosenfield Avatar answered Sep 19 '22 19:09

Adam Rosenfield


memcmp compares a number of bytes. strcmp and the like compare strings.

You kind of cheat in your example because you know that both strings are 5 characters long (plus the null terminator). However, what if you don't know the length of the strings, which is often the case? Well, you use strcmp because it knows how to deal with strings, memcmp does not.

memcmp is all about comparing byte sequences. If you know how long each string is then yeah, you could use memcmp to compare them, but how often is that the case? Rarely. You often need string comparison functions because, well... they know what a string is and how to compare them.

As for any other issues you are experiencing it is unclear from your question and code. Rest assured though that strcmp is better equipped in the general case for string comparisons than memcmp is.

like image 45
Ed S. Avatar answered Sep 21 '22 19:09

Ed S.


strcmp():

  • It is used to compare the two string stored in two variable, It takes some time to compare them. And so it slows down the process.

strncmp():

  • It is very much similar to the previous one, but in this one, it compares the first n number of characters alone. This also slows down the process.

memcmp():

  • This function is used compare two variables using their memory. It doesn't compare them one by one, It compares four characters at one time. If your program is too concerned about speed, I recommend using memcmp().
like image 33
Arvind kr Avatar answered Sep 19 '22 19:09

Arvind kr