Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Function memcmp in C programming

September 18, 2013 Posted by: GPA Views: 26904 0 comments

Problem

How does memcmp() work? Write simple program using this function.

Solution

In this section, you will know how to use memcmp() function of header file <string.h>.

Declaration:

int memcmp( const void *firstString, const void *secondString, size_t n)

This function compares a specified number of characters (n) of firstString with the character of secondString. It returns a value greater than null if the firstString is greater than the secondString, returns 0 if the first and second arguments are equal and returns a value less than 0 if the firstString argument is less than the secondString.

Example:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>//for function memcmp
//main function
int main(int argc, char *argv[])
{
char str1[]="I love to swim";//firstString argument
char str2[]="I love to play football";//secondString argument
printf("globalproganswer.com\n");
printf("First string: %s\n",str1);
printf("Second string: %s\n\n",str2);
//1 - str1 is greater than the str2
//0 - str1 and str2 equal
//-1 - str2 is greater than the str1
printf("memcmp(str1, str2, 9) = %2d\n",memcmp( str1, str2, 9 ));
printf("memcmp(str1, str2, 11) = %2d\n",memcmp( str1, str2, 11 ));
printf("memcmp(str1, str2, 16) = %2d\n",memcmp( str2,str1, 16 ));
printf("\n");//new line
system("PAUSE");//delay
return 0;
}

Output

Function memcmp

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe