This function compares two strings lexicographically.
Syntax:
int strcmp(const char *str1, const char *str2);
Parameters:
- str1: A pointer to the first string.
- str2: A pointer to the second string.
Return Value:
- if str1 is lexicographically less than str2 then value will be less than 0.
- if str1 is equal to str2 then value will be 0.
- if str1 is lexicographically greater than str2 then value will be greater than 0.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "My";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal.\n");
} else if (result < 0) {
printf("str1 is less than str2.\n");
} else {
printf("str1 is greater than str2.\n");
}
return 0;
}
Output:
str1 is less than str2.