Latest web development tutorials

C library functions - memcmp ()

C standard library - <string.h> C standard library - <string.h>

description

C library functionsint memcmp (const void * str1, const void * str2, size_t n)) of the storage area and storage area str1the firstnbytesstr2are compared.

statement

Here is memcmp () function's declaration.

int memcmp(const void *str1, const void *str2, size_t n)

parameter

  • str1 - point to a memory block pointer.
  • str2 - point to a memory block pointer.
  • n - the number of bytes to be compared to.

return value

  • If the return value <0, the str1 is less than str2.
  • If the return value> 0 indicates less than str2 str1.
  • If the return value = 0, then str1 is equal to str2.

Examples

The following example demonstrates the use of memcmp () function.

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

int main ()
{
   char str1[15];
   char str2[15];
   int ret;

   memcpy(str1, "abcdef", 6);
   memcpy(str2, "ABCDEF", 6);

   ret = memcmp(str1, str2, 5);

   if(ret > 0)
   {
      printf("str2 小于 str1");
   }
   else if(ret > 0) 
   {
      printf("str1 小于 str2");
   }
   else 
   {
      printf("str1 等于 str2");
   }
   
   return(0);
}

Let's compile and run the above program, which will result in the following:

str2 小于 str1

C standard library - <string.h> C standard library - <string.h>