C strcmp() Function Tutorial

In this section we will learn what the strcmp() function is and how to use it in C.

Comparing String in C: strcmp() Function

Sometimes we want to compare and see whether two character-strings are the same or one is larger than the other!

For example:

Is “test” is equal to “test”?

Is “Hello” is equal to “goodbye”?

This is where we can use the `strcmp` function.

Note: `cmp` stands for `compare` and of course `str` stands for `string`.

The prototype of this function exists in the `string.h` header and so we need to include the header file in order to use the function.

strcmp() Function Syntax

Here’s the prototype of the function:

int strcmp( const char* lhs, const char* rhs );

strcmp() Function Parameters

  • The first parameter of this function is the address of the first character-string involved in the comparison operation.
  • The second parameter of this function is the address of the second character-string involved in the comparison operation.

strcmp() Function Return Value

  • The return value of this function is an integer and based on the arguments we put in the function, there are three different integer values that might return from the function:
  1. 0: if the returned value of the function is 0, it means both the first and the second arguments are exactly the same.
  2. Positive value: if the first argument in the function is greater than the second argument, the result value is a positive integer.
  3. Negative value: if the first argument in the function is smaller than the second argument, the result value is a negative integer.

Note: the comparison is done character by character which it means the first character of the first argument is compared with the first character of the second argument, then the second characters are compared and so on… and as long as the characters are the same, this comparison goes on to the end of character-strings. If there was a difference between characters, the comparison will stop right there and either a positive or a negative value will return. If all the characters in both character-strings were the same, the end result will be 0.

Example: comparison of strings in C

#include <stdio.h>
#include <string.h>
int main() {

    char * pFirst = "Hello";
    char * pSecond = "Hello";

    int result = strcmp(pFirst, pSecond);


    printf("The result is: %d", result);
    return 0;
}

Output:

The result is: 0

Example: compare two strings in C

#include <stdio.h>
#include <string.h>
int main() {

    char * pFirst = "Hello";
    char * pSecond = "Hello to you";

    int result = strcmp(pFirst, pSecond);


    printf("The result is: %d", result);
    return 0;
}

Output:

The result is: -1

In this example both character-strings are the same until the end of `Hello` word but the second argument has `to you` part but the first argument doesn’t and so the result is -1 (which means the first argument is smaller than the second one.

Example: comparing characters in C

#include <stdio.h>
#include <string.h>
int main() {

    char * pFirst = "hello";
    char * pSecond = "Hello";

    int result = strcmp(pFirst, pSecond);


    printf("The result is: %d", result);
    return 0;
}

Output:

The result is: 1

You should know that in programming, the capital letters come before those lowercased letters and so for this reason the letter `h` is considered greater than the `H` letter and so the result is a positive value for this example.

Note: Usually it’s a bad idea to compare two strings that might have capital letters in them. The ideal comparison is to first convert the entire characters into either capital letters or lowercase letters and then do the comparison. For this reason, there are functions that we can use to convert the letters of a character-string into a lowercase or uppercase letters and these functions are covered in the toupper() and tolower() sections.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies