C String Equality: Using strcmp()
In C language, to determine if two strings are equal, you can use the standard library function strcmp(). This function compares the content of two strings and returns 0 if they are the same, otherwise it returns a non-zero value.
The sample code is shown below:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "Hello";
if (strcmp(str1, str2) == 0) {
printf("两个字符串相等\n");
} else {
printf("两个字符串不相等\n");
}
return 0;
}
In the example code above, we define two strings, str1 and str2, and then use the strcmp() function to compare their contents. If the return value is 0, it means the two strings are equal; otherwise, they are not equal.