strcasecmp() function in C
In this article, we will discuss the strcasecmp() function in C programming language with its syntax, parameter, example, and output.
The string comparison method strcasecmp() in C programming compares two strings while considering the characters' case (uppercase or lowercase). As a result, when comparing strings, this function considers both 'A' and 'a' to be the same character.
The strcasecmp() function works with strings that have null terminations. The string arguments to the function are expected to contain a null character ('\0') marking the end of the string.
Syntax:
It has the following syntax:
int strcasecmp(const char *string1, const char *string2);
Parameters:
S1: Pointer to the first string with a null end.
S2: Pointer to the second string with a null end.
Return Value:
Value | Meaning |
Less than 0 | string1 less than string2 |
0 | string1 equivalent to string2 |
Greater than 0 | string1 greater than string2 |
Note:The strcasecmp() function is not a part of the C standard library (C89/C90). Instead, several C libraries offer it, including the Microsoft C Runtime Library (MSVCRT) for Windows and the GNU C Library (glibc) for Linux.
Example:
Let us see an example program to understand the strcasecmp() function in C.
#include <stdio.h>
#include <strings.h> //header file
int main() {
//string 1 pointer
const char *str1 = "tutorial";
//string 2 pointer
const char *str2 = "examples";
//strcasecmp() function
int result = strcasecmp(str1, str2);
if (result == 0) {
printf("The strings are equal.\n");
} else if (result < 0) {
printf("'%s' is less than '%s'.\n", str1, str2);
} else {
printf("'%s' is greater than '%s'.\n", str1, str2);
}
return 0;
}
Output:
![strcasecmp() function in C](https://static.tutorialandexample.com/c/strcasecmp-function-in-c1.png)
Benefits of strcasecmp() function in C:
The strcasecmp() function comes in handy when you need to compare strings without considering letter cases. The strcasecmp() function has the following advantages:
Code simplification: The strcasecmp() function will help you make your code more precise and shorter. You can allow the function to handle the case-insensitive comparison directly rather than manually changing all characters to a particular case before comparison.
Faster than manual comparison: Although there may be more efficient techniques for particular use cases, strcasecmp() is typically adequate. Making your case-insensitive comparison technique could result in more written code and poorer performance.
Case-insensitive comparison: The main benefit of using the strcasecmp() function is that it enables you to compare strings without doing so. In situations when you don't want the comparison to be impacted by the case, it means that strings with varied letter cases will be viewed as equal.
Locale-aware comparison: The strcasecmp() function can handle case-insensitive comparisons for characters unique to specific languages or geographical regions.
Widely accessible and uniform: The <strings.h> header defines the standard C library function strcasecmp(). It is dependable and portable since it works with many systems and compilers.