
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 <h2>输出</h2>
<pre class="brush:php;toolbar:false;">str1 == str2 upto 9 characters!
str1 > str2!
C++免费学习笔记(深入):立即使用
在学习笔记中,你将探索 C++ 的入门与实战技巧!











