Difference Between strcmp() and strcmpi() Function in C

Get Certified in C Programming and Take Your Skills to the Next Level

String manipulation is a fundamental aspect of C programming, and one common task is comparing strings. Whether you’re searching for a specific substring, sorting a list of names, or validating user inputs, string comparison is at the heart of many operations.

In C, two primary functions, strcmp() and strcmpi(), come into play when it comes to comparing strings, but they serve distinct purposes.

The strcmp() function is a stalwart for case-sensitive string comparisons. It meticulously examines every character within two strings, discerning even the slightest variations. On the other hand, the strcmpi() function offers a more relaxed approach, performing case-insensitive comparisons, where ‘A’ is equivalent to ‘a,’ and ‘B’ is equal to ‘b.’

In this article, we’ll delve into the intricacies of these two C library functions, comparing their strengths and weaknesses. We’ll explore their usage, behavior, and the scenarios in which one should be chosen over the other.

By the end, you’ll have a clear understanding of when to employ strcmp() and when strcmpi() is the better option for your string comparison needs. Let’s embark on this journey of understanding case-sensitive and case-insensitive string comparisons in the world of C programming.

The strcmp() Function in C

When comparing two strings, the strcmp() function takes into account case. Based on the ASCII value of each character, it compares strings character by character.

The syntax of strcmp() is:

int strcmp(const char *str1, const char *str2);

It returns:

  • 0 if both strings are equal
  • A negative number if str1 < str2
  • A positive number if str1 > str2

Let’s look at an example:

#include <stdio.h>
#include <string.h>

int main() {
  char str1[15] = "String1";
  char str2[15] = "String2";

  int result = strcmp(str1, str2);

  if(result < 0) {
    printf("str1 is less than str2\n"); 
  }
  else if(result > 0) {
    printf("str1 is greater than str2\n");
  }
  else {
    printf("str1 is equal to str2\n");
  }
  
  return 0;
}

Output:
str1 is less than str2

This prints “str1 is less than str2” as string comparison is case-sensitive.

Some key properties of strcmp():

  • Comparison is case-sensitive
  • ASCII values of characters are compared
  • Useful for sorting, finding min/max strings etc.

The strcmpi() Function in C

The “string.h” header file contains the definition of the strcmpi() function, a built-in function in the C language. The only distinction between the strcmpi() and strcmp() functions is that the former is case sensitive while the latter is not, making strcmpi() identical to strcmp().

The syntax is:

int strcmpi(const char *str1, const char *str2);

This function accepts two string parameters to compare:

  • str1 – The first string passed to the function. This will be compared against the second string.
  • str2 – The second string passed to the function. This will be compared against the first string.

The function returns an integer value indicating the result of the comparison:

Return ValueMeaning
0str1 and str2 are identical
Negative numberstr1 length < str2 length
Positive numberstr1 length > str2 length

Examples

str1str2Return ValueReason
“hello”“hello”0Strings are identical
“cat”“tiger”-3“cat” length (3) < “tiger” length (5)
“antelope”“deer”4“antelope” length (8) > “deer” length (4)

Note: The strcmpi() function is a non-standard one that might not be found in the C standard library.

Let’s look at an example:

#include <stdio.h>
#include <string.h>

int main() {

  char str1[15] = "String1";
  char str2[15] = "string1";

  int result = strcmpi(str1, str2);

  if(result == 0) {
    printf("str1 is equal to str2\n");
  }

  return 0;
}

Output:
str1 is equal to str2

This prints “str1 is equal to str2” as case is ignored by strcmpi().

Some key points about strcmpi():

  • Comparison ignores upper/lower case differences
  • Case is internally normalized before comparing
  • Useful for comparing user input or configurations

Key Differences Between strcmp() and strcmpi() in C

Featurestrcmp()strcmpi()
Case sensitivityCase-sensitive comparisonCase-insensitive comparison
NormalizationNo normalizationNormalizes to same case before comparing
Comparison methodDirectly compares ASCII valuesNormalizes case, then compares ASCII values
PerformanceFaster as direct comparisonSlower due to normalization
Return valueNegative if str1 < str2, Positive if str1 > str2, 0 if equal0 if equal, non-zero if unequal
Use casesSorting, finding min/max stringsUser input validation, configuration value comparison

When to Use Each Function

Based on their behavior, here are some guidelines on when to use each function:

  • Use strcmp() when case matters, like for sorting or finding min/max
  • Use strcmpi() when case must be ignored, like user input validation
  • Use strcmp() for comparing identifiers, tokens, symbols etc.
  • Use strcmpi() for comparing configuration values, file paths etc.

Conclusion

String comparison is fundamental to many C programs. Both strcmp() and strcmpi() allow for the comparison of two strings, with the main difference being that strcmpi() ignores case. Keeping their exact behavior in mind helps us choose the right function based on whether case-sensitivity matters for our specific use case.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *