strcmp函數用于比較兩個字符串的大小,返回值有三種情況:
以下是使用strcmp比較字符串大小的示例代碼:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "abc";
char str2[] = "def";
int result = strcmp(str1, str2);
if (result == 0) {
printf("str1 is equal to str2\n");
} else if (result > 0) {
printf("str1 is greater than str2\n");
} else {
printf("str1 is less than str2\n");
}
return 0;
}
輸出結果為:
str1 is less than str2
在此示例中,字符串"abc"小于字符串"def",所以strcmp返回一個負數。