C 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 :

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 :

int main(int argc, char *argv[])
	int outcome, len, l1, l2
		printf("Incorrect number of arguments.");
	/* 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);
	else if(outcome<0)
		printf("First less than second.");
		printf("First greater than second.");

C Online Test

« Previous Chapter Next Function »

Like/Share Us on Facebook 😋