
strncmp() 和 strcmp 使用 ASCII 字符比较来比较两个字符串。 strncmp 采用一个附加参数作为要与字符串进行比较的字符数。它非常有用,因为如果字符串无效,那么 strcmp 将无法完成其操作。 strcmp 在字符串末尾搜索结束字符('/0')以完成其操作。 strncmp 使用 no。字符来结束其操作,因此是安全的。
#include <stdio.h>
int main() {
char str1[] = "TutorialsPoint";
char str2[] = "Tutorials";
// Compare strings with strncmp()
int result1 = strncmp(str1, str2, 9);
if(result1 == 0){
printf("str1 == str2 upto 9 characters!\n");
}
// Compare strings using strcmp()
int result2 = strcmp(str1, str2);
if(result2 == 0){
printf("str1 == str2!\n");
} else {
if(result2 > 0){
printf("str1 > str2!\n");
} else {
printf("str1 < str2!\n");
}
}
return 0;
}str1 == str2 upto 9 characters! str1 > str2!
以上就是strncmp()和strcmp()之间的C/C++区别的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号