C语言 C 中的 memcmp、strcmp 和 strncmp 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13095513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:12:47  来源:igfitidea点击:

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

cstringc-strings

提问by und3rd06012

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

我用 C 写了一小段代码来测试 C 中的memcmp()strncmp()strcmp()函数。

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.

我第一次尝试strcmp()但不幸的是,当它达到将“原子”一词放入文件中的程度时,它并没有停止并继续,但是当我使用memcmp()或 时,strncmp()它停止了,我很高兴。

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.

但后来我想,如果有一个字符串,其中前 5 个字母是 a,t,o,m,s 并且这些字母后面是其他字母,那该怎么办。

Unfortunately,my thoughts were right as I tested it using the above code by initializing word1to "atomsaaaaa" and word2to 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().

不幸的是,我的想法是正确的,因为我使用上面的代码通过初始化word1为“atomsaaaaa”和word2原子来测试它,memcmp()并且strncmp()在 if 语句中返回 0。另一方面,strcmp()它没有。看来我必须使用strcmp().

回答by Adam Rosenfield

In short:

简而言之:

  • strcmpcompares null-terminated C strings
  • strncmpcompares at most N characters of null-terminated C strings
  • memcmpcompares binary byte buffers of N bytes
  • strcmp比较以空字符结尾的 C 字符串
  • strncmp比较最多 N 个以空字符结尾的 C 字符串的字符
  • memcmp比较 N 个字节的二进制字节缓冲区

So, if you have these strings:

所以,如果你有这些字符串:

const char s1[] = "atoms
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
##代码####代码####代码##"; // extra null bytes at end const char s2[] = "atoms##代码##abc"; // embedded null byte const char s3[] = "atomsaaa";

Then these results hold true:

那么这些结果成立:

##代码##

回答by Ed S.

memcmpcompares a number of bytes. strcmpand the like compare strings.

memcmp比较多个字节。 strcmp之类的比较字符串

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 strcmpbecause it knows how to deal with strings, memcmpdoes not.

你在你的例子中有点作弊,因为你知道两个字符串都是 5 个字符长(加上空终止符)。但是,如果您不知道字符串的长度(通常是这种情况)怎么办?好吧,您使用它是strcmp因为它知道如何处理 stringsmemcmp而不是。

memcmpis all about comparing byte sequences. If you know how long each string is then yeah, you could use memcmpto 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.

memcmp都是关于比较字节序列。如果您知道每个字符串的长度,那么是的,您可以memcmp用来比较它们,但这种情况多久发生一次?很少。您经常需要字符串比较函数,因为……它们知道字符串是什么以及如何比较它们。

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

至于您遇到的任何其他问题,您的问题和代码都不清楚。请放心,strcmp在字符串比较的一般情况下,它比memcmp现在更好。

回答by Arvind kr

strcmp():

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():

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.
  • 它与前一个非常相似,但在这个中,它仅比较前 n 个字符。这也会减慢进程。

memcmp():

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().
  • 此函数用于使用它们的内存比较两个变量。它不是一一比较,而是一次比较四个字符。如果你的程序太在意速度,我推荐使用 memcmp()。

回答by prashant

strncmp and memcmp are same except the fact that former takes care of NULL terminated string.

strncmp 和 memcmp 是相同的,除了前者处理 NULL 终止的字符串。

回答by RAZ_MUH_TAZ

For strcmp you'll want to be only comparing what you know are going to be strings however sometimes this is not always the case such as reading lines of binary files and there for you would want to use memcmp to compare certain lines of input that contain NUL characters but match and you may want to continue checking further lengths of input.

对于 strcmp,您只想比较您知道将是字符串的内容,但是有时情况并非总是如此,例如读取二进制文件的行,并且您可能希望使用 memcmp 来比较某些包含的输入行NUL 字符但匹配,您可能希望继续检查输入的更多长度。