How can C language determine if two char strings are equal?
In C programming language, the strcmp function can be used to determine if two char strings are equal.
The prototype of the function strcmp is: int strcmp(const char *str1, const char *str2)
This function compares two strings, str1 and str2, and returns an integer value. If the two strings are equal, it returns 0; if str1 is less than str2, it returns a negative number; if str1 is greater than str2, it returns a positive number.
Here is an example code demonstrating how to use the strcmp function to determine if two char strings are equal:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "Hello";
char str3[] = "World";
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;
}
output result:
str1 and str2 are equal
str1 and str3 are not equal