C strcmp() (original) (raw)

Last Updated : 02 Apr, 2025

In C, **strcmp() is a built-in library function used to compare two strings lexicographically. It takes two strings (array of characters) as arguments, compares these two strings lexicographically, and then returns some value as a result.

Let's take a look at an example:

C `

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

int main(){ char* s1 = "Geeks"; char* s2 = "Geeks";

// Printing the return value of the strcmp()
printf("%d", strcmp(s1, s2));

return 0;

}

`

**Explanation: In this code, two strings, **s1 and **s2, are declared with the same value "**Geeks". The strcmp() function compares these two strings and returns 0 since they are identical.

This article covers the syntax of count() function, its working, and common usage with examples.

Table of Content

Syntax of strcmp()

The strcmp() function is defined inside the ****<string.h>** header file as:

**strcmp(s1, s2);

**Parameters:

**Return Value:

The strcmp() function returns three different values after the comparison of the two strings which are as follows:

How strcmp() works?

C strcmp() function works by comparing the **two strings lexicographically. It means that it compares the ASCII value of each character till the non-matching value is found or the NULL character is found. The working of the C strcmp() function can be described as follows:

**1. It starts with comparing the ASCII values of the first characters of both strings.

**2. If the first characters in both strings are equal, then this function will check the second character, if they are also equal, then it will check the third, and so on till the first unmatched character is found or the NULL character is found.

**3. If a **NULL character is found, the function **returns zero as both strings will be the same.

compare-similar-strings-using-strcmp

**4. If a non-matching character is found,

compare-larger-string-with-smaller-string-using-strcmp

compare-smaller-string-with-larger-string-using-strcmp

All of these three cases are demonstrated in the below examples.

Examples of strcmp() in C

The below examples demonstrate the behaviour of strcmp() in C programs:

Find Identical Strings

C `

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

int main() {

char s1[] = "g f g";
char s2[] = "g f g";

// Using strcmp() to compare s2 and s2
int res = strcmp(s1, s2);

if (res == 0)
    printf("Equal");
else
    printf("Unequal");

return 0;

}

`

**Explanation: In this code, strcmp is used to compare two strings, **s1 and **s2. Since both strings are identical ("g f g"), **strcmp returns 0, indicating the strings are equal. The program then prints "**Equal". If the strings were different, strcmp would return a non-zero value, and the program would print "**Unequal".

Find Lexicographically Greater String

C `

#include<stdio.h> #include<string.h> int main() { // z has greater ASCII value than g char first_str[] = "zfz"; char second_str[] = "gfg";

int res = strcmp(first_str, second_str);

if (res==0)
    printf("Equal");
else
    printf("Unequal");

return 0;

}

`

**Explanation: In this code, strcmp compares the two strings first_str ("zfz") and second_str ("gfg"). Since 'z' has a greater ASCII value than 'g', strcmp returns a positive value, indicating the strings are unequal. The program prints "Unequal" because of the lexicographical comparison based on ASCII values.

Sort the Array of Strings

As strcmp() compare two strings, it can be used to sort the array of strings in the desired order by using it as a comparator function in qsort().

C `

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

int comp(const void* a, const void* b) {

// Using strcmp() for comparing two strings
return strcmp(*(const char**)a, *(const char**)b); 

}

int main() { const char* arr[] = { "Rahul", "Vikas", "Mukesh" };

int n = sizeof(arr) / sizeof(arr[0]); 

// Sort the given arr 
qsort(arr, n, sizeof(arr[0]), comp); 

// Print the sorted arr
for (int i = 0; i < n; i++) 
    printf("%s\n", arr[i]);

return 0; 

}

`