发布网友 发布时间:2022-04-23 06:15
共1个回答
热心网友 时间:2023-09-26 11:08
int
strcmp
(p1,
p2)
const
char
*p1;
const
char
*p2;
{
register
const
unsigned
char
*s1
=
(const
unsigned
char
*)
p1;
register
const
unsigned
char
*s2
=
(const
unsigned
char
*)
p2;
unsigned
reg_char
c1,
c2;
do
{
c1
=
(unsigned
char)
*s1++;
c2
=
(unsigned
char)
*s2++;
if
(c1
==
'\0')
return
c1
-
c2;
}
while
(c1
==
c2);
return
c1
-
c2;
}
这是strcmp的源代码。其中循环部分如果c1=c2进循环,直到字符串s1结束或c1不等于c2(即s1与s2有对应字符不同时)为止,返回值均为c1-c2。只有当循环结束时c1依旧等于c2,c1-c2才等于0,否则不等于0.