strcmp一般用法
#include <stdio.h>
#include<string.h>
int main()
{
char arr1[] = { "abcde" };
char arr2[] = { "abcd" }; //vs2022编译器下如果相同为0,如果arr1>arr2,结果则为1,反之则为-1;
printf("%d", strcmp(arr1, arr2));
return 0;
}
strcmp在vs编译器下的实现
#include<assert.h>
#include<stdio.h>
#include<string.h>
int my_strcmp(char* dest, char* src)
{
assert(*dest && *src);
while (*dest == *src)
{
if (*dest == '\0')
{
return 0;
}
*dest++;
*src++;
}
if (*dest > *src)
return 1;
else
return - 1;
}
int main()
{
char arr1[] = { "abcde" };
char arr2[] = { "abcd" };
printf("%d\n",my_strcmp(arr1, arr2));
}
strcmp函数在其他编译器下的实现
#include<assert.h>
#include<string.h>
#include<stdio.h>
int my_strcmp(char* dest, char* src)
{
assert(*dest && *src);
while (*dest == *src)
{
if (*dest == '\0')
{
return 0;
}
*dest++;
*src++;
}
return(*dest - *src);//如果dest>src就是正数,反之;
}
int main()
{
char arr1[] = { "abcde" };
char arr2[] = { "abcd" };
printf("%d\n",my_strcmp(arr1, arr2));
}
有不同看法或者补充,私信老大爷,感谢!
标签:src,函数,dest,char,arr2,arr1,strcmp From: https://blog.51cto.com/u_15835473/5827657