When comparing strings in C, what do you use? The strcmp
function checks whether two strings are the same — like asking your program, “Are these two the same?” Of course, a program can’t decide whether cats or dogs are cuter, but it *can* tell the difference between “HELLO” and “hello”. In this article, we’ll explore how strcmp
works, how to use it, and common mistakes to watch out for.
1. What is the strcmp function?
The strcmp
function compares two strings character by character and returns an integer based on the result. Here’s how it behaves:
- 0: The strings are exactly the same
- Positive value: The first string comes *after* the second string in dictionary order
- Negative value: The first string comes *before* the second string in dictionary order
So, not only can you check if two strings are identical, but you can also determine which one comes first. Here’s a simple example of how to use it in code:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
printf("Result of strcmp: %dn", result);
return 0;
}
This code compares “Hello” and “World” and prints the result. It’s important to note that strcmp
is case-sensitive, so “HELLO” and “hello” will be treated as different strings.
2. How strcmp Works Under the Hood
The behavior of strcmp
is simple yet powerful. It compares two strings character by character from the beginning, and as soon as it finds a mismatch, it returns the difference between those characters. This mechanism is also useful for alphabetical comparisons — for example, when comparing “apple” and “banana”, strcmp
will compare ‘a’ and ‘b’ first and return a negative value.
Here’s another program example to help you understand how strcmp
works:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "apple";
char str2[] = "banana";
int result = strcmp(str1, str2);
if (result == 0) {
printf("The strings match.n");
} else if (result < 0) {
printf("str1 comes before str2.n");
} else {
printf("str1 comes after str2.n");
}
return 0;
}
This code compares “apple” and “banana”. As expected, strcmp
tells us that “apple” comes before “banana” in dictionary order. This makes it a handy tool for sorting strings alphabetically.

3. Examples of Using strcmp
1. Verifying User Input
strcmp
is useful when you want to compare user input with existing data — such as checking if a password matches the correct one.
#include <stdio.h>
#include <string.h>
int main() {
char password[] = "secret";
char input[256];
printf("Enter your password: ");
scanf("%s", input);
if (strcmp(password, input) == 0) {
printf("Password match.n");
} else {
printf("Incorrect password.n");
}
return 0;
}
This program compares the entered password with the correct one using strcmp
.
2. Sorting in Dictionary Order
strcmp
is also handy for sorting strings alphabetically. When sorting a list of words, you can use strcmp
to determine which word should come first.
4. Alternatives to strcmp
While strcmp
is very useful, there are cases where other functions might be more appropriate depending on what you’re trying to achieve.
strncmp: Compare Part of a String
If you only want to compare part of a string, strncmp
is a better option. For example, if you just want to compare the first 3 characters of two strings, use this function.
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "apple";
char str2[] = "apricot";
if (strncmp(str1, str2, 3) == 0) {
printf("The first 3 characters match.n");
} else {
printf("The first 3 characters do not match.n");
}
return 0;
}
memcmp: Compare Raw Memory
memcmp
is used to compare raw memory rather than just strings. It’s useful when dealing with binary data or non-text memory blocks.
5. Common Mistakes When Using strcmp
1. Ignoring Case Sensitivity
strcmp
is case-sensitive. That means “HELLO” and “hello” are considered different strings. If you need to perform a comparison that ignores case, consider using strcasecmp
instead (note: this function may vary depending on your environment).
2. Comparing NULL Pointers
If you try to use strcmp
to compare a NULL pointer, your program will likely crash. Always make sure that the strings you’re comparing have been properly initialized.
Conclusion
strcmp
is an essential function in C programming that makes string comparison simple and effective. We’ve walked through real-world examples like password checks and alphabetical sorting to show how it’s used in practice. If you’re working with strings in your programs, you’ll definitely find this function incredibly useful — give it a try and see how it fits into your code!