Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C strncmp() Function

Posted in C Tutorial
Updated on Oct 06, 2024
By Mari Selvan
👁ī¸ 383 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
C strncmp() Function

Photo Credit to CodeToFun

🙋 Introduction

In C programming, comparing strings is a common operation, and there are standard functions tailored for different scenarios.

The strncmp() function is one such function that allows you to compare a specified number of characters in two strings.

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

💡 Syntax

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

Syntax
Copied
Copy To Clipboard
int strncmp(const char *s1, const char *s2, size_t n);

This function takes two strings, s1 and s2, and an additional parameter n representing the maximum number of characters to compare.

📄 Example

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

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

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

  // Compare the first 7 characters of the strings
  int result = strncmp(str1, str2, 7);

  // Output the result
  if (result == 0) {
    printf("The first 7 characters are equal.\n");
  } else {
    printf("The first 7 characters are not equal.\n");
  }

  return 0;
}

đŸ’ģ Output

Output
The first 7 characters are equal.

🧠 How the Program Works

In this example, the strncmp() function compares the first 7 characters of the strings "Hello, World!" and "Hello, C!" and prints the result.

↩ī¸ Return Value

The strncmp() function returns an integer less than, equal to, or greater than zero if the first n characters of s1 are found, respectively, to be less than, to match, or be greater than the first n characters of s2.

📚 Common Use Cases

The strncmp() function is useful when you want to compare only a specific portion of two strings. It's commonly used in scenarios where you need to check prefixes or substrings with a limited length.

📝 Notes

  • Ensure that the value of n passed to strncmp() is within the valid length of the strings to avoid undefined behavior.
  • The comparison is case-sensitive.

đŸŽĸ Optimization

The strncmp() function is optimized for comparing a specific number of characters in two strings. However, for performance-critical applications, consider profiling your code and exploring alternative approaches if needed.

🎉 Conclusion

The strncmp() function in C is a versatile tool for comparing strings with a limited length. It provides control over the number of characters to compare, making it suitable for various string comparison scenarios.

Feel free to experiment with different strings and explore the behavior of the strncmp() 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
11 months ago

If you have any doubts regarding this article (C strncmp() 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