×

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

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

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.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 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.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 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.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

4 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute read.