0

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

3
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by prvnkmr449
1

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.