Syntax of strcmp() function in C
The syntax of the strcmp() function in the C programming language is as follows:
Int strcmp(const char *s1, const char *s2);
In this function, we will compare two strings in lexicographical order, and it will return us the answer by comparing the strings s1 and s2.
If ‘s1’ is lexicographically less than ‘s2’, it will return a negative integer value.
If ‘s1’ is lexicographically greater than ‘s2’, it will return a positive integer value.
If ‘s1’ and ‘s2’ are equal, it will return 0.
Parameters of strcmp() function in C
The function called strcmp() in the C programming language takes in two parameters:
'*s1' is a constant pointer that points to the initial string.
In addition, '*s2' is a constant pointer directing to the second string.
Both s1 and s2 are pointers that refer to unmodifiable characters. The function called strcmp() accepts inputs in the form of pointers to character arrays that require comparing.
Return value of strcmp() function in C
In the C programming language, the strcmp() function is a strong tool for comparing strings. It analyzes the characters in two strings and returns an integer value. In C, the strcmp() method produces an integer value based on the first mismatched character between the two strings.
The strcmp() function yields three distinct values following the comparison of the two strings, as follows:
1. Zero ( 0 )
If both strings are the same, the return value is zero. That is, elements in both strings with the same index are equivalent.
Example:
C++
#include<stdio.h>
#include<string.h>
int main() {
char string1[] = "Ninja";
char string2[] = "Ninja";
// Calling strcmp() and storing the return value
int r = strcmp(string1, string2);
if (r == 0)
printf("Strings are equal");
else
printf("Strings are unequal");
printf("\nValue returned by strcmp() is: %d", r);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
Strings are equal
Value returned by strcmp() is: 0
Explanation:
Strings string1 and string2 are declared and initialized in the above code. They are subsequently run using the strcmp() function. The function's return value is saved in the variable r. A conditional statement is used to determine whether or not two strings are the same by determining whether or not the rrva is equal to zero.
2. Greater than Zero ( > 0 )
If the ASCII value of the first unmatched character in the left-hand side string (string1) is greater than the equivalent character in the right-hand side string (string2), the return value is greater than zero. The difference between the ASCII values of the first unmatched characters in the strings, i.e., (string1-string2), yields the resultant value.
Example:
C++
#include<stdio.h>
#include<string.h>
int main() {
char string1[] = "scotland";
char string2[] = "india";
int r = strcmp(string1, string2);
// Condition for checking equality
if (r == 0)
printf("Strings are equal");
else
printf("Strings are unequal");
printf("\nValue returned by strcmp() is: %d", r);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
Strings are unequal
Value returned by strcmp() is: 10
Description:
Strings string1 and string2 are declared and initialized in the above code. They are subsequently processed by the strcmp() function. The function's return value is saved in the variable r. The first mismatched character in the strings is discovered at index 0, where the characters in both strings are 's' and 'i'. Both characters have ASCII values of 115 and 105, respectively. As a result, the difference in ASCII value equals 10.
3. Lesser than Zero ( < 0 )
If the ASCII value of the first unmatched character in the left-hand side string (string1) is less than that of the corresponding character in the right-hand side string (string2), the return value is less than zero. The difference between the ASCII values of the first unmatched characters in the strings, i.e., (string1-string2), yields the resultant value.
Example:
C++
#include<stdio.h>
#include<string.h>
int main() {
char string1[] = "Sunday";
char string2[] = "Zombie";
int rvalue = strcmp(string1, string2);
// Condition for checking equality
if (rvalue == 0)
printf("Strings are equal");
else
printf("Strings are unequal");
printf("\nValue returned by strcmp() is: %d", rvalue);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
Strings are unequal
Value returned by strcmp() is: -7
Description:
The first unmatched character in the strings is found at index 0 in the given code, where characters in both strings are S and Z, respectively. Both characters have ASCII values of 83 and 90, respectively. As a result, the difference in ASCII value is -7.
Also see, Short int in C Programming
How to Use strcmp in C
Here are the steps to use strcmp in C:
-
Include the Header File: Include the <string.h> header file at the beginning of your program.
-
Declare Strings: Declare the strings you want to compare using the char data type.
-
Call strcmp Function: Use the strcmp() function to compare the strings. It takes two arguments: the strings you want to compare.
-
Check the Return Value: strcmp() returns an integer value:
- If the return value is 0, the strings are equal.
- If the return value is less than 0, the first string is lexicographically less than the second string.
- If the return value is greater than 0, the first string is lexicographically greater than the second string.
-
Perform Actions Based on Comparison: Based on the return value of strcmp(), you can perform further actions in your program, such as printing a message or executing specific code.
How strcmp() in C Works?
The strcmp() function in C compares two null-terminated strings lexicographically. It is part of the C standard library, defined in the <string.h> header. The function prototype is:
int strcmp(const char *str1, const char *str2);
How It Works:
-
Comparison: strcmp() compares each character of the strings pointed to by str1 and str2 sequentially.
-
Return Values:
- Returns 0 if the strings are equal.
- Returns a negative value if str1 is less than str2 (i.e., str1 comes before str2 in lexicographic order).
- Returns a positive value if str1 is greater than str2 (i.e., str1 comes after str2 in lexicographic order).
-
Termination: The comparison stops as soon as a difference is found or the end of either string is reached.
Examples of strcmp() in C
Example 1: Identical Strings
C
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "hello";
char str2[] = "hello";
int result = strcmp(str1, str2);
printf("Result: %d\n", result);
return 0;
}
You can also try this code with Online C Compiler
Run Code
Output
0
Explanation: Both strings are identical, so strcmp() returns 0.
Example 2: str1 Less Than str2
C
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "apple";
char str2[] = "banana";
int result = strcmp(str1, str2);
printf("Result: %d\n", result);
return 0;
}
You can also try this code with Online C Compiler
Run Code
Output
-1
Explanation: "apple" is lexicographically less than "banana", so strcmp() returns a negative value.
Example 3: str1 Greater Than str2
C
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "grape";
char str2[] = "apple";
int result = strcmp(str1, str2);
printf("Result: %d\n", result);
return 0;
}
You can also try this code with Online C Compiler
Run Code
Output
1
Explanation: "grape" is lexicographically greater than "apple", so strcmp() returns a positive value.
Example 4: Different Length Strings
C
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "short";
char str2[] = "shorter";
int result = strcmp(str1, str2);
printf("Result: %d\n", result);
return 0;
}
You can also try this code with Online C Compiler
Run Code
Output
-1
Explanation: "short" is shorter and lexicographically less than "shorter", so strcmp() returns a negative value.
Example 5: Different Characters
C
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "zebra";
char str2[] = "zebras";
int result = strcmp(str1, str2);
printf("Result: %d\n", result);
return 0;
}
You can also try this code with Online C Compiler
Run Code
Output
-1
Explanation: "zebra" is lexicographically less than "zebras" because it is shorter and terminates earlier, so strcmp() returns a negative value.
Frequently Asked Questions
What does the strcmp str1 str2 function return?
The strcmp function's return value is 0 when the two strings are equal. It returns a value less than 0 when str1 compares less than str2 and a value greater than 0 when str1 compares greater than str2. It's important not to make any additional assumptions about the value returned by strcmp.
What library is strcmp in C?
The C standard string library has a built-in function called strcmp(). This library contains commonly used string manipulation operations. <string.h> header file invokes a call to this library in a program.
Why use strcmp()?
strcmp() is used to compare two strings lexicographically to determine their relative order or check for equality.
What value does strcmp() return?
strcmp() returns 0 if the strings are equal, a negative value if the first string is less, and a positive value if greater.
What is the strcmp expression?
The strcmp expression compares two strings character by character and returns an integer indicating their lexicographic difference.
What is the strcmp formula?
The formula is: strcmp(str1, str2) = first differing character code of str1 - str2 or 0 if equal.
Conclusion
We extensively discussed the strcmp function in c language. We learned how this function works, its output values and its prototype. The strcmp function in c is very versatile and suitable for comparing strings lexicographically. We also went through a sample code involving the strcmp function in c.
We hope this blog has helped you enhance your knowledge about the topic strcmp function in C. If you like to learn more, you can check out our articles:
🌐 Static Functions in C
🌐 Operator Precedence in C
🌐 Implicit return type in C
🌐 String Compare in C
🌐 Ternary Operator in C
Refer to our Guided Path on Code360 to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, and many more! If you wish to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!
If you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundles for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!