The function memcmp () in C

The function int memcmp (const void * str1, const void * str2, size_t n)) compares the first n bytes of two strings str1 and str2.

The function int memcmp (const void * str1, const void * str2, size_t n)) compares the first n bytes of two strings str1 and str2.

Declare the function memcmp () in C

Below is the declaration for memcmp () in C:

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

Parameters

str1 - This is the pointer to a memory block.

str2 - This is a pointer to a memory block.

n - This is the number of bytes to be compared.

Returns the value

If

If the value> 0 is returned, this function indicates that str2 is shorter than str1.

If the value = 0 is returned, this function indicates that str1 is equal to str2.

For example

The following C program illustrates the usage of memcmp () in C:

 #include #include 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 ( "Chuoi str2 la ngan hon chuoi str1" ); } else if ( ret < 0 ) { printf ( "Chuoi str1 la ngan hon chuoi str2" ); } else { printf ( "Chuoi str1 la bang chuoi str2" ); } return ( 0 ); } 

Compiling and running the above C program will result:

Picture 1 of The function memcmp () in C

According to Tutorialspoint

Previous article: Function memchr () in C

Next lesson: Function memcpy () in C

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile