codescracker


c

C memcmp()



« Previous Chapter Next Function »


memcmp()

The memcmp() function compares the first count characters of the arrays pointed to by buf1 and buf2.

The memcmp() function returns an integer that is interpreted as indicated here in the following table :

Value Meaning
Less than zero buf1 is less than buf2
Zero buf1 is equal to buf2
Greater than zero buf1 is greater than buf2

memcmp() Syntax

Following is the syntax of the memcmp() function :

#include<string.h>
int memcmp(const void *buf1, const void *buf2, size_t count);

memcmp() Example

Following program shows the outcome of a comparison of its two command line arguments :

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
	int outcome, len, l1, l2
	if(argc!=3)
	{
		printf("Incorrect number of arguments.");
		exit(1);
	}
	/* find the length of shortest string */
	l1 = strlen(argv[1]);
	l2 = strlen(argv[2]);
	len = l1 < l2 ? l1:l2;
	outcome = memcmp(argv[1], argv[2], len);
	if(!outcome)
		printf("Equal");
	else if(outcome<0)
		printf("First less than second.");
	else
		printf("First greater than second.");
	getch();
}

« Previous Chapter Next Function »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test