新闻  |   论坛  |   博客  |   在线研讨会
C语言字符篇(三)字符串比较函数
电子禅石 | 2020-07-04 19:11:52    阅读:9615   发布文章

C语言字符篇(三)字符串比较函数    

            

            #include <string.h>    
int strcmp(const char *s1, const char *s2);    比较字符串s1和s2    
int strncmp(const char *s1, const char *s2, size_t n);    比较字符串s1和s2前n个字符    
 
如果两个字符一样大,返回值为 0
如果s1>s2,则返回正值,
如果s1<s2,则返回负值.
 
-------------------------------------------------------
int main(int argc, char **argv) 
{    const char *s1="hello";    
    const char *s2="hello";    
    const char *s3="aello";    
    int ret1,ret2;


    ret1=strncmp(s1,s2,strlen(s1));
    ret2=strncmp(s1,s3,strlen(s1));


    printf("%d\n",ret1);    //0
    printf("%d\n",ret2);    //7  return 0;
}


*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
属于自己的技术积累分享,成为嵌入式系统研发高手。
推荐文章
最近访客