淡泊明志 宁静致远
分类: C/C++
2006-11-25 14:55:34
【C语言库函数源代码】
【本程序在Dev C++ 4.9.9.2 下编译通过】
/*
Compares count bytes of memory starting at
buffer1 and buffer2 and
比较内存区域buffer1和buffer2的前count个字节。当buffer1 <
buffer2时,返回值 < 0;
*/
int my_memcmp(const void *buffer1,const void *buffer2,int
count)
{
if (!count)
return(0);
while (
--count && *(char *)buffer1 == *(char *)buffer2)
{
buffer1 = (char
*)buffer1 + 1;
buffer2 = (char
*)buffer2 + 1;
}
return( *((unsigned char *)buffer1)
- *((unsigned char
*)buffer2) );
}
void Print(char * str1,char *str2,int t)
{
if(t > 0)
printf("\n%s Upper Than
%s\n",str1,str2);
else if(t < 0)
printf("\n%s Lower Than
%s\n",str1,str2);
else
printf("\n%s Equal
%s\n",str1,str2);
}
int main()
{
char *str1=
"ammana";
char *str2 =
"babi";
Print(str1,str2,my_memcmp(str1,str2,3));
Print(str2,str1,my_memcmp(str2,str1,3));
Print(str2,str2,my_memcmp(str2,str2,3));
system("pause");
return 0;
}