Detailed Explanation of strcmp() Function in C Language

C String Comparison: strcmp() Function

The strcmp(first_string, second_string) function is used to compare two strings. If the two strings are equal, it returns 0.

👇Click to Claim👇
👉C Language Knowledge Resource Collection

In the example below, we use the gets() function to read strings from the console.

#include <stdio.h>
#include <string.h>
int main() {  char str1[20], str2[20];  printf("Enter 1st string: ");  gets(str1); // Read string from console  printf("Enter 2nd string: ");  gets(str2);  if (strcmp(str1, str2) == 0)      printf("Strings are equal");  else      printf("Strings are not equal");  return 0;}

Output:

Enter 1st string: hello
Enter 2nd string: hello
Strings are equal

In the above example, we used the strcmp() function to compare the two strings str1 and str2. If they are equal, it prints “Strings are equal”; otherwise, it prints “Strings are not equal”.

Detailed Explanation of strcmp() Function in C Language


 Popular Recommendations
  • C Language Tutorial – Detailed Explanation of strcat() Function

  • C Language Algorithm – “Roman Numerals to Integer” Algorithm Problem

  • C Language Exercises – Programming Exercise (13)

Leave a Comment