×

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on the result after the comparison, it could go back to a high-quality or negative integer value.

This feature also can go back to 0. This function is represented as int memcmp(const void *str1, const void *str2, size_t n). In this characteristic, the laptop examines whether the dimensions of the primary element of item str1 are less than, extra than, or identical to the size of n initial factors of object str2. This characteristic is defined in the string.h header file. Consequently, you must import the header file into your program to use the functions in your code.

Declaration of the memcmp() function in C:

The syntax of the memcmp() function in C is given below –

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

Header file of the memcmp() function in C:

Header file is very important for execute any program. The header file of the memcmp() function in C is a string. h. So, it is written below:

include <string.h>

Parameters of the memcmp() function in C:

str1 – str1 is a pointer to a memory block. This pointer declared within the function points to the position of the first object compared to the second object.

str2 – str2 is a pointer to a block of memory.

n - n is the number of bytes to compare.

Return value of the memcmp() function in C:

  1. The return value is a positive integer:  Memory block one is greater than memory block 2.
  2. The return value is a negative integer: Memory block one is less than memory block 2.
  3. The return value is 0: The size of memory block one must be equal to the size of memory block 2.

Example 1: The example of the memcmp() function in C is given below –

#include<stdio.h>
#include<string.h>
int main()
{
char str1[15];
char str1[15];
int ret;
memcpy(str1,”abcdefg”, 7);
memcpy(str2,”ABCDEFG”, 7);
ret = memcmp(str1, str2, 5);
if(ret > 0)
{
printf (“str2 is less than str1”);
}
else if(ret > 0)
{
printf (“str1 is less than str2”);
}
else {
printf (“str2 is equal to str1”);
}
return 0;
}
}	

Result: We compile the above written program and also run this program. After compilation, the output is:

str2 is less than str1
The size of array1 is equal to the size of array2

Explanation of the above program: The value returned by memcmp() is negative. The second element of the array is different. So memcmp() evaluates which element is lesser. Therefore a negative integer is returned.

Example 2: The give another example of the memcmp() function in C is given below –

#include <stdio.h>  
#include <string.h>  
int main ()  
{  
int result = 0;  
int ary1[ ] = {1,2,3};  
int ary2[ ] = {1,2,3};  
result = memcmp(ary1, ary2, 6);  
if(result > 0)  
{  
prints("The size of array1 is greater than the size of array2");  
}  
else if(result < 0)  
{  
printf("The size of array1 is less than the size of array2");  
}  
else  
{  
printf("The size of array1 is equal to the size of array2");  
}  
return 0;  
}  

Result: We compile the above written program and also run this program. After compilation, the output is:

The size of array1 is equal to the size of array2

Explanation of the above program: Both arrays are the same in the example above. So memcmp() returns 0, which is stored in the variable.

Example 3: The give another example of the memcmp() function in C is given below –

#include <stdio.h>  
#include <string.h>  
int main ()  
{  
int result = 0;  
int ary1[] = {5,5,7,12};  
int ary2[] = {5,5,7,12};  
result = memcmp(ary1, ary2,1300);  
if(result > 0)  
{  
printf("The size of array1 is greater than the size of array2");  
}  
else if(result < 0)  
{  
printf("The size of array1 is less than the size of array2");  
}  
else  
{  
printf("The size of array1 is equal to the size of array2");  
}  
return 0;  
}  

Result: We compile the above written program and also run this program. After compilation, the output is:

The size of array1 is greater than the size of array2

Explanation of the above program: The second element of the array is different. So memcmp() evaluates which element is bigger and the resulting value is a negative integer.

So, here we discuss about the memcmp() function in C. We share some example and output value of it.


Related Topics

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 minutes read.

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

2 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Pure Virtual Function in C

Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below. In c++ the member function that is defined in the...

4 minutes read.

Static function in C

The functions in the C programming language are by default global. This means the programmer can easily access the function which is outside from the file where it was initially...

3 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

Decimal to Hexadecimal in C

Decimal to Hexadecimal in C Let us first understand the definitions of decimal and hexadecimal. In algebra, a decimal number is defined as a number whose whole number part and a...

3 minutes read.