diff --git a/C Crash Course/13 String/Prelesson/code/mystrcmp/VSstrcmp.c b/C Crash Course/13 String/Prelesson/code/mystrcmp/VSstrcmp.c new file mode 100644 index 0000000..67a52e3 --- /dev/null +++ b/C Crash Course/13 String/Prelesson/code/mystrcmp/VSstrcmp.c @@ -0,0 +1,29 @@ +//这是我能理解的格式 +int mystrcmp(char* str1, char* str2) { + + int ret = 0; + + while ((ret = ((unsigned char)*str1 - (unsigned char)*str2)) == 0 && *str1) { + ++str1, ++str2; + } + + return ret; +} + + +//原版在这里 +int __cdecl strcmp( + const char* src, + const char* dst +) +{ + int ret = 0; + + while ((ret = *(unsigned char*)src - *(unsigned char*)dst) == 0 && *dst) + { + ++src, ++dst; + } + + //这里不太理解,那个大神理解了可以给我讲一下,谢谢 + return ((-ret) < 0) - (ret < 0); // (if positive) - (if negative) generates branchless code +} \ No newline at end of file diff --git a/C Crash Course/13 String/Prelesson/code/mystrcmp/main.c b/C Crash Course/13 String/Prelesson/code/mystrcmp/main.c new file mode 100644 index 0000000..06d8e64 --- /dev/null +++ b/C Crash Course/13 String/Prelesson/code/mystrcmp/main.c @@ -0,0 +1,11 @@ + + +int main() { + + char* str1 = "Hello "; + char* str2 = "Hello"; + + printf("%d\n", mystrcmp(str1, str2)); + + return 0; +} \ No newline at end of file diff --git a/C Crash Course/13 String/Prelesson/code/mystrcmp/mystrcmp.c b/C Crash Course/13 String/Prelesson/code/mystrcmp/mystrcmp.c new file mode 100644 index 0000000..22596c1 --- /dev/null +++ b/C Crash Course/13 String/Prelesson/code/mystrcmp/mystrcmp.c @@ -0,0 +1,7 @@ +int mystrcmp(char* str1, char* str2) { + + while (*str1 == *str2 && *str1) + ++str1, ++str2; + + return (*str1 - *str2); +} \ No newline at end of file