在C語(yǔ)言中,可以使用strcmp函數(shù)來(lái)比較兩個(gè)字符串是否相等。strcmp函數(shù)的原型如下:
int strcmp(const char *str1, const char *str2);
該函數(shù)接受兩個(gè)參數(shù),分別是要比較的兩個(gè)字符串。如果兩個(gè)字符串相等,則返回值為0;如果str1小于str2,則返回值小于0;如果str1大于str2,則返回值大于0。以下是一個(gè)示例代碼:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "Hello";
char str3[] = "Hi";
if (strcmp(str1, str2) == 0) {
printf("str1 and str2 are equal.\n");
} else {
printf("str1 and str2 are not equal.\n");
}
if (strcmp(str1, str3) == 0) {
printf("str1 and str3 are equal.\n");
} else {
printf("str1 and str3 are not equal.\n");
}
return 0;
}
運(yùn)行上述代碼,輸出結(jié)果如下:
str1 and str2 are equal.
str1 and str3 are not equal.