×

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

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.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

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.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

User Defined Functions in C

C Functions: A function is defined as a block of code that is used to perform some specific task. In functions, we use flower brackets ({}) which contain the set of programming...

5 minutes read.

Git Hooks

Git Hooks Overview Just like other version control systems, Git has its way to deliver customized scripts whenever some important activity occurs. The hooks act just like the triggers or catalysts...

4 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Header files in C

Header files in C In the C programming language, header files are present, which have an extension of ‘.h’, and it consists of macro definitions, declarations, and so on that are...

4 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes 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.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.