ITeye博客

C函数源码解读:strcmp

longsy · 2009-03-17 13:09 · 编程所学 · Markdown

strcmp函数是在string.h文件中声明 strcmp函数的作用是将两个字符串按词典顺序进行比较


extern int    strcmp(const char *, const char *);

OpenBSD对strcmp实现如下


int
strcmp(const char *s1, const char *s2)
{
       //两字符串相等的情况,如果两个字符串中所有的字符相等,则返回0
        while (*s1 == *s2++)    //两个字符相等
                if (*s1++ == 0) //达到字符串末尾
                        return (0);
       //不相等,则返回值为第一次不相等时的两字符之差
        return (*(unsigned char *)s1 - *(unsigned char *)--s2);
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。