memcmp() — Compare bytes

Standards

Standards / Extensions C or C++ Dependencies
ISO C
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3
both  

Format

#include <string.h>

int memcmp(const void *buf1, const void *buf2, size_t count);

General description

The memcmp() built-in function compares the first count bytes of buf1 and buf2.

The relation is determined by the sign of the difference between the values of the leftmost first pair of bytes that differ. The values depend on EBCDIC encoding. This function is not locale sensitive.

Returned value

Indicates the relationship between buf1 and buf2 as follows:
Value
Meaning
< 0
The contents of the buffer pointed to by buf1 less than the contents of the buffer pointed to by buf2
= 0
The contents of the buffer pointed to by buf1 identical to the contents of the buffer pointed to by buf2
> 0
The contents of the buffer pointed to by buf1 greater than the contents of the buffer pointed to by buf2

Example

CELEBM12
/* CELEBM12                                      

   This example compares first and second arguments passed to                   
   main to determine which, if either, is greater.                              
                                                                                
 */                                                                             
#include <stdio.h>                                                              
#include <string.h>                                                             
                                                                                
int main(int argc, char ** argv)                                                
{                                                                               
  int  len;                                                                     
  int  result;                                                                  
                                                                                
  if ( argc != 3 )                                                              
  {                                                                             
     printf( "Usage: %s string1 string2\n", argv[0] );                          
  }                                                                             
  else                                                                          
  {                                                                             
     /* Determine the length to be used for comparison */                       
     if (strlen( argv[1] ) < strlen( argv[2] ))                                 
       len = strlen( argv[1] );                                                 
     else                                                                       
       len = strlen( argv[2] );                                                 
                                                                                
     result = memcmp( argv[1], argv[2], len );                                  
                                                                                
     printf( "When the first %i characters are compared,\n", len );             
     if ( result == 0 )                                                         
       printf( "\"%s\" is identical to \"%s\"\n", argv[1], argv[2] );           
     else if ( result < 0 )                                                     
       printf( "\"%s\" is less than \"%s\"\n", argv[1], argv[2] );              
     else                                                                       
       printf( "\"%s\" is greater than \"%s\"\n", argv[1], argv[2] );           
  }                                                                             
}                                                                               

Output

If the program is passed the arguments firststring and secondstring, you would obtain following:
When the first 11 characters are compared,
“firststring” is less than “secondstring”

Related information