Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C memcmp() Function

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

Photo Credit to CodeToFun

🙋 Introduction

In C programming, the memcmp() function is a standard library function defined in the <string.h> header.

It is used for comparing two blocks of memory, specifically designed for comparing character arrays or strings.

The memcmp() function provides a way to compare consecutive bytes of memory and determine whether they are equal or which one is greater.

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

💡 Syntax

The syntax for the memcmp() function is as follows:

Syntax
Copied
Copy To Clipboard
int memcmp(const void* ptr1, const void* ptr2, size_t num);
  • ptr1: A pointer to the first block of memory.
  • ptr2: A pointer to the second block of memory.
  • num: The number of bytes to compare.

📄 Example

Let's dive into an example to illustrate how the memcmp() function works.

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

int main() {
  char str1[] = "Hello, C!";
  char str2[] = "Hello, D!";

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

  if (result == 0) {
    printf("The first 7 characters are equal.\n");
  } else if (result < 0) {
    printf("The first differing character in str1 is less than str2.\n");
  } else {
    printf("The first differing character in str1 is greater than str2.\n");
  }

  return 0;
}

đŸ’ģ Output

Output
The first 7 characters are equal.

🧠 How the Program Works

In this example, the memcmp() function is used to compare the first 7 characters of two strings, "Hello, C!" and "Hello, D!". The result is then used to determine the relationship between the two strings.

↩ī¸ Return Value

The function returns an integer representing the relationship between the two memory blocks:

  • Returns 0 if the contents of both memory blocks are equal.
  • Returns a negative value if the first differing byte in ptr1 is less than the corresponding byte in ptr2.
  • Returns a positive value if the first differing byte in ptr1 is greater than the corresponding byte in ptr2.

📚 Common Use Cases

The memcmp() function is commonly used in scenarios where direct memory comparison is required, such as sorting strings or checking for equality between two strings with a specified number of characters.

📝 Notes

  • The memcmp() function treats the memory blocks as arrays of unsigned char, allowing for byte-wise comparison.
  • It is important to ensure that the specified number of bytes (num) does not exceed the size of the memory blocks to prevent undefined behavior.

đŸŽĸ Optimization

The memcmp() function is typically optimized for performance by leveraging low-level memory comparison instructions provided by the hardware. No additional optimization is usually needed.

🎉 Conclusion

The memcmp() function in C is a powerful tool for comparing blocks of memory, particularly useful when dealing with character arrays or strings. It provides a versatile mechanism for determining the relationship between two memory blocks.

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

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