strncmp是一个预定义的库函数,存在于string.h文件中,它用于比较两个字符串并显示哪个字符串更大。
此函数比较两个字符串。它返回两个字符串中第一个不匹配字符的ASCII差异。
int strcmp (string1, string2);
如果差异等于零,则 string1 = string2。
如果差异为正,则 string1> string2。
如果差异为负,则 string1 <string2。

此函数用于比较两个字符串的前 n 个字符。
strncmp ( string1, string2,2)
#include<stdio.h>
#include<string.h>
void main(){
//Declaring two strings//
char string1[25],string2[25];
int value;
//Reading string 1 and String 2//
printf("Enter String 1: ");
gets(string1);
printf("Enter String 2: ");
gets(string2);
//Comparing using library function//
value = strncmp(string1,string2,4);
//If conditions//
if(value==0){
printf("%s is same as %s",string1,string2);
} else if(value>0) {
printf("%s is greater than %s",string1,string2);
} else {
printf("%s is less than %s",string1,string2);
}
}Enter String 1: Tutorials Enter String 2: Point Tutorials is greater than Point
以上就是写一个C程序,使用strncmp库函数来比较两个字符串的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号