Could You Please Explain me what is memcmp? What does the 'size_t' means to?

Recommended Answers

All 3 Replies

memcmp compares the first n bytes of strings s1 and s2.
Syntax

int memcmp(const void *s1,const void *s2,size_t n);

Return value
1. <0 if s1<s2
2. =0 if s1==s2
3. >0 if s1>s2
Example

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

int main()
{
   char *buf1 = "aaa";
   char *buf2 = "bbb";
   char *buf3 = "ccc";

   int stat;

   stat = memcmp(buf2, buf1, strlen(buf2));
   if (stat > 0)
      printf("buffer 2 is greater than buffer 1\n");
   else
      printf("buffer 2 is less than buffer 1\n");

   stat = memcmp(buf2, buf3, strlen(buf2));
   if (stat > 0)
      printf("buffer 2 is greater than buffer 3\n");
   else
      printf("buffer 2 is less than buffer 3\n");

   return 0;
}

size_t is size in byte to compare string as memcmp compare first n byte.

size_t Type used for memory object sizes, and n represent the size of memory to compare

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.