Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C strcmp() Function

Posted in C Tutorial
Updated on Jan 14, 2024
By Mari Selvan
👁ī¸ 178 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
C strcmp() Function

Photo Credit to CodeToFun

🙋 Introduction

In C programming, the strcmp() function is a standard library function used for comparing strings.

It allows you to determine whether two strings are equal or determine their relative order lexicographically.

In this tutorial, we'll explore the usage and functionality of the strcmp() function in C.

💡 Syntax

The signature of the strcmp() function is as follows:

Syntax
Copied
Copy To Clipboard
int strcmp(const char *s1, const char *s2);

This function takes two strings, s1 and s2, and performs a lexicographical comparison.

📄 Example

Let's delve into an example to illustrate how the strcmp() function works.

strcmp.c
Copied
Copy To Clipboard
#include <stdio.h>
#include <string.h>

int main() {
  const char * str1 = "Hello, World!";
  const char * str2 = "Hello, Universe!";

  // Compare strings
  int result = strcmp(str1, str2);

  // Output the result
  if (result == 0) {
    printf("The strings are equal.\n");
  } else if (result < 0) {
    printf("str1 is less than str2.\n");
  } else {
    printf("str1 is greater than str2.\n");
  }

  return 0;
}

đŸ’ģ Output

Output
str1 is greater than str2.

🧠 How the Program Works

In this example, the strcmp() function compares the strings "Hello, World!" and "Hello, Universe!" and prints the result.

↩ī¸ Return Value

The strcmp() function returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. The comparison is based on the ASCII values of the characters in the strings.

📚 Common Use Cases

The strcmp() function is useful when you need to compare strings for equality or determine their order. It is commonly used in sorting algorithms, searching for specific strings, or checking user input.

📝 Notes

  • The comparison is case-sensitive. For case-insensitive comparison, consider using strcasecmp() in POSIX systems or _stricmp() in Windows.

đŸŽĸ Optimization

The strcmp() function is optimized for lexicographical string comparison. For performance-critical applications, you may want to explore alternative approaches or consider implementing your optimized version based on specific requirements.

🎉 Conclusion

The strcmp() function in C is a fundamental tool for string comparison. It provides a standardized and efficient way to compare strings, enabling you to make decisions based on their order or equality.

Feel free to experiment with different strings and explore the behavior of the strcmp() function in various scenarios. Happy coding!

👨‍đŸ’ģ Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
5 months ago

If you have any doubts regarding this article (C strcmp() Function) please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy