strcmp in C – How to Compare Strings in C
Md. Fahim Bin Amin
Comparing strings is a common task in most programming languages. In C, you can use the strcmp function to handle string comparisons.
In this article, I will show you practical examples of the strcmp function, and offer insights into how it compares strings, what its return values mean, and how to use it effectively.
You’ll also see some best practices to help optimize your code.
What are Strings in C?
Before we discuss the strcmp function, it is important to understand the basics of strings in C.
A string is an array of characters terminated by a null character (‘\0’).
Strings are usually represented either using character pointers ( char * ) or character arrays ( char  ).
What is the strcmp() function of C, and how does it work?
The strcmp() function is part of the standard C library ( string.h ). Its primary purpose is to compare the characters of the two strings in sequence until it finds a mismatch or until the end of the strings is reached (that is, the null character ‘\0’). In our programming world, we call it lexicographic order-based searching.
The function prototype for strcmp is as follows:
Here’s what the parameters above mean:
- s1 denotes the first string to be compared.
- s2 denotes the second string to be compared.
strcmp() is like a game that compares two words. It helps us to identify whether a word comes before or after another word in the dictionary.
- If the first word (s1) comes before the second word (s2) in the dictionary, strcmp() gives a negative number.
- If the first word (s1) comes after the second (s2) word in the dictionary, strcmp() gives a positive number.
- If both words are the same, strcmp() gives the number 0.
strcmp() compares the corresponding characters from both strings based on their ASCII values, which are numeric codes that represent each character.
Therefore, if the ASCII value of the first differing character in the first string is less than the ASCII value of the corresponding character in the second string, strcmp() returns a negative number. This indicates that the first string comes before the second string in the dictionary.
If the ASCII value of the first differing character in the first string is greater than the ASCII value of the corresponding character in the second string, strcmp() returns a positive number. This indicates that the first string comes after the second string in the dictionary.
If the two strings are equal up to the end of the shorter string, strcmp() returns a negative, zero, or positive value depending on whether the longer string has a character with a smaller, equal, or greater ASCII value than the null character.
Actually, the C compiler implements this logic of comparing the ASCII values of characters in two strings and returning the result accordingly.
strcmp() Function Example #1 – Basic String Comparison
Let me now explain each of the lines from the code given above.
Here, I have taken two different strings as two different character arrays, as we do not have access to direct strings in the C programming language.
In the first character string ( str1 ), I have stored the string apple . In the second character string ( str2 ), I have stored the string banana . As the function strcmp() gives a boolean output (true/false, or 0/1), I have taken another int variable named result to store the boolean value ( 1 for true and 0 for false ).
The strcmp() function compares the two strings, and finds out that the first string apple comes before the second string banana . Therefore, the function strcmp() returns a negative value indicating that the first string is less than the second string.
Based on the value in result , it prints String 1 is less than string 2. .
If you are confused about «the string comes before or after another string» then let me explain to you a bit more.
When we say that the first string comes before the second string, we mean that if the two strings were listed in a dictionary or a sorted list of words, the first string would appear before the second string.
In the case of the program above, the first string is «apple» , and the second string is «banana» . If we were to look up these two words in a dictionary, we would find that «apple» appears before «banana» , which means that the first string comes before the second string.
The strcmp() function compares the two strings character by character and determines which string comes first in the dictionary based on the ASCII values of the characters.
In this case, the first character of «apple» (which is ‘a’ ) has a lower ASCII value (ASCII value for a = 97) than the first character of «banana» (which is ‘b’ and the ASCII value for b = 98), so strcmp() returns a negative value, indicating that the first string comes before the second string.
I hope this clears up any confusion regarding what the phrase «one string comes after/before another string» means.
strcmp() Function Example #2 – Case-insensitive String Comparison
Here I converted both strings into lowercase. I used a for loop to change all the characters in the string to lowercase using the function tolower() . The for loops convert both strings to lowercase by iterating over each character of the strings using an index variable i , and calling the tolower() function on each character to convert it to lowercase.
After converting both strings into lowercase, I called the function strcmp() to check whether both strings are equal or not, like earlier. I stored the output of the strcmp() function in a new variable named result , like earlier.
The if statement checks the value of result and prints out the corresponding message depending on whether the strings are equal, or which string is less or greater. Here, the two strings become equal after converting them to lowercase. Thus, the first if statement executes and provides the output.
Keep in mind that, after converting both strings to lowercase, the value of str1 is «apple» , which is the same as the value of str2 in the ASCII.
Therefore, when the strcmp() function is called, it returns 0 , which indicates that the strings are equal. This can be useful when we want to compare strings that may differ in case but should be considered equal.
strcmp() Best Practices
Here are some best practices to follow when using the strcmp() function:
- Always include the string.h header when using strcmp() .
- To compare case-insensitive strings, use a custom function like strcasecmp() or a standard library function like stricmp() .
- Be cautious when comparing strings that may contain non-ASCII characters, as strcmp() uses the difference in ASCII values for comparisons, which might not work as expected for non-ASCII characters. In such cases, consider using a Unicode-aware comparison function or library.
If you want to learn more about ASCII characters, check out this article.
Alternative String Comparison Functions
Apart from strcmp() , there are other string comparison functions available in the C standard library:
- strncmp() : Compares up to a specified number of characters of two strings. It is useful when you want to compare only a portion of the strings.
- strcoll() : This is quite interesting. Let me explain more about this.
The strcoll() function is like a game that helps us compare two words or strings, just like strcmp() . But strcoll() is designed to handle strings that contain non-ASCII characters or require language-specific comparisons, such as words from different languages.
strcoll() uses the rules for comparing words that are specific to the language or region, based on the current locale setting of the computer. In other words, it compares words according to the language-specific collation rules of the locale, which may be different from the standard ASCII ordering used by strcmp() .
For example, in the Spanish language, the word «cañón» (which means «canyon» in English) is sorted after «casa» (which means «house» in English), because the letter «ñ» is considered a separate letter in the Spanish alphabet, and comes after «n».
So, strcoll() can be useful for comparing words from different languages or regions that have specific collation rules, and we use this function for this kind of specific need.
In this article, we’ve explored the strcmp() function in C, how it compares strings, and its return values. We’ve also looked at examples and best practices for using strcmp effectively.
With this knowledge, you can easily compare strings in C like a pro. Remember to consider alternative comparison functions like strncmp() and strcoll() when working with specific use cases, such as partial or locale-specific string comparisons.
If this article helps you in any way, then let me know via Twitter or LinkedIn. You can also follow me on GitHub and check my YouTube channel, and my website. You can also buy me a coffee if you want to support me.
Эта функция сравнивает символы двух строк, string1 и string2 . Начиная с первых символов функция strcmp сравнивает поочередно каждую пару символов, и продолжается это до тех пор, пока не будут найдены различные символы или не будет достигнут конец строки.
Первая сравниваемая Си-строка.
Вторая сравниваемая Си-строка.
Функция возвращает несколько значений, которые указывают на отношение строк:
Нулевое значение говорит о том, что обе строки равны.
Значение больше нуля указывает на то, что строка string1 больше строки string2 , значение меньше нуля свидетельствует об обратном.
Функция strcmp начинает сравнивать по одному символу и как только будут найдены первые неодинаковые символы, функция проанализирует числовые коды этих символов. Чей код окажется больше, та строка и будет считаться большей.
strcmp() in C
The strcmp() function is a built-in library function in the C language. Its basic role is to compare two character arrays or strings terminated by null value (C-strings) lexicographically. The strcmp() function uses two character arrays as parameters and returns an integer value.
What is strcmp() in C?
The function strcmp() is a built-in library function of the C language. The <string.h> header file contains its prototyping and definition. The function takes strings as arguments and traverses them to compare and evaluate their equality. It matches strings lexicographically, which signifies that it checks for every character at every index in both the strings.
The comparison begins from the first character of strings and carries on for every character until any unmatched or NULL character is encountered.
Syntax of strcmp() in C
The syntax for using strcmp() function in C is:
where string1 and string2 are the char array variables.
Parameters of strcmp() in C
The strcmp() function takes two parameters string1 and string2 , that are pointers to the character arrays required to be compared.
The prototype of strcmp() function in C as described in the string.h header file is:
- The strcmp() matches the elements of strings string1 and string2 lexicographically.
- The return value is difference between the first unmatched pairs of characters that occur in string1 and string2 .
Return Value of strcmp() in C
The strcmp() function in C returns an integer value calculated according to the first mismatched character among the two strings. There are three types of return values generated by the strcmp() function.
- Zero (0): The return value is equal to zero if both the strings are the same. That is, elements at the same index in both the strings are equivalent.
Explanation: In the above code, two strings, string1 and string2 , are declared and initialized respectively. They are then passed through strcmp() function. The return value of the function gets stored in the variable rvalue . A conditional statement is used to check whether strings are the same or not by verifying whether the rvalue is equal to zero or not.
- Greater than zero ( >0 ): The return value is greater than zero if the ASCII value of the first unmatched character of the string on the left-hand side ( string1 ) is greater than that of the corresponding character in the right-hand side string ( string2 ). The resultant value is the difference between the ASCII values of the first unmatched characters of the strings, i.e., ( string1-string2 ).
Explanation: In the above code, two strings, string1 and string2 , are declared and initialized, respectively. They are then passed through the strcmp() function. The return value of the function gets stored in the variable rvalue . The first unmatched character in the strings is found at index 0 where characters in both the strings are ‘ s ’ and ‘ i ’, respectively. The ASCII values of both the characters are 115 and 105 , respectively.
Hence, the difference in the ASCII value turns out as 10 . A conditional statement is used to check whether strings are the same or not by verifying whether rvalue is equal to zero or not.
- Less than zero ( <0 ): The return value is less than zero if the ASCII value of the first unmatched character of the string on the left-hand side ( string1 ) is less than that of the corresponding character in the right-hand side string ( string2 ). The resultant value is the difference between the ASCII values of the first unmatched characters of the strings, i.e., ( string1-string2 ).
Explanation: In the above code, the first unmatched character in the strings is found at index 0, where characters in both the strings are S and Z , respectively. The ASCII values of both the characters are 83 and 90 , respectively. Hence, the difference in the ASCII value turns out as -7 .
Implementation of strcmp() Function Using an User-Defined Function
Explanation: In the above code, two strings string1 and string2 are declared and initialized respectively. They are then passed through a user-defined function, matchStr() . In the matchStr() function, strcmp() function is called and the return value of the function gets stored in the variable rvalue . A conditional statement is used to check whether strings are the same or not by verifying whether rvalue is equal to zero or not.
C library function — strcmp()
The C library function int strcmp(const char *str1, const char *str2) compares the string pointed to, by str1 to the string pointed to by str2.
Following is the declaration for strcmp() function.
str1 − This is the first string to be compared.
str2 − This is the second string to be compared.
This function return values that are as follows −
if Return value < 0 then it indicates str1 is less than str2.
if Return value > 0 then it indicates str2 is less than str1.
if Return value = 0 then it indicates str1 is equal to str2.
The following example shows the usage of strcmp() function.
Let us compile and run the above program that will produce the following result −