strncmp函数字符串比较

来源:互联网 发布:学编程 app 编辑:程序博客网 时间:2024/05/19 10:36
摘自linux内核4.11.1源码string.c
linux/lib/string.c
Copyright (C) 1991, 1992  Linus Torvalds

头文件:#include <string.h>
作用: 比较两个字符串cs和ct
参数:
cs:字符串一
ct:字符串二
count:比较的字节数
返回值:
1、若cs和ct相等,返回0
2、若cs大于ct,返回正值
3、若cs小于ct,返回负值
/** * strncmp - Compare two length-limited strings * @cs: One string * @ct: Another string * @count: The maximum number of bytes to compare */int strncmp(const char *cs, const char *ct, size_t count){unsigned char c1, c2;while (count) {c1 = *cs++;c2 = *ct++;if (c1 != c2)return c1 < c2 ? -1 : 1;if (!c1)break;count--;}return 0;}


原创粉丝点击