C语言中的memcmp,strcmp和strncmp有什么区别? [英] What is the difference between memcmp, strcmp and strncmp in C?
问题描述
我用C语言编写了这小段代码,以测试C语言中的 memcmp()
strncmp()
strcmp()
函数.>
这是我编写的代码:
#include< stdio.h>#include< stdlib.h>#include< string.h>int main(){char * word1 ="apple",* word2 ="atoms";如果(strncmp(word1,word2,5)== 0)printf("strncmp结果.\ n");如果(memcmp(word1,word2,5)== 0)printf("memcmp result.\ n");如果(strcmp(word1,word2)== 0)printf("strcmp结果.\ n");}
有人会因为我对这三个功能感到困惑而向我解释这些差异吗?
我的主要问题是我有一个文件对其行进行标记化,问题是当我对文件中的原子"一词进行标记化时,我必须停止标记化的过程.
我首先尝试了 strcmp()
,但是不幸的是,当到达文件中放置原子"一词时,它并没有停止并继续,但是当我使用了 memcmp()
或 strncmp()
停止了,我很高兴.
但是后来我想,如果存在一个字符串,其中前5个字母是a,t,o,m,s,然后是其他字母,那么该怎么办?
不幸的是,我的想法是正确的,因为我使用上述代码通过将 word1
初始化为"atomsaaaaa",将 word2
初始化为原子和 memcmp()
strncmp()
返回0,而另一方面 strcmp()
却没有.看来我必须使用 strcmp()
.
简而言之:
因此,如果您有以下字符串:
const char s1 [] ="atoms \ 0 \ 0 \ 0 \ 0";//末尾多余的空字节const char s2 [] ="atoms \ 0abc";//嵌入空字节const char s3 [] ="atomsaaa";
然后这些结果成立:
strcmp(s1,s2)== 0//strcmp在空终止符处停止strcmp(s1,s3)!= 0//字符串不同strncmp(s1,s3,5)== 0//字符串的前5个字符相同memcmp(s1,s3,5)== 0//前5个字节相同strncmp(s1,s2,8)== 0//字符串从空终止符开始是相同的memcmp(s1,s2,8)!= 0//前8个字节不同
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()
.
In short:
strcmp
compares null-terminated C stringsstrncmp
compares at most N characters of null-terminated C stringsmemcmp
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
这篇关于C语言中的memcmp,strcmp和strncmp有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!