It will check whether the initial “n” characters of the variable indicated as the first string are fewer than, equivalent to, or larger than the initial “n” characters of the other variable i.e., string. The memcmp method returns a negative, zero, or a positive integer in the C Language. This article has been utilized to understand the memcmp() function of C in the code. Thus, let’s get started with the terminal.
Example 01:
Let’s take a new start with the creation of a C file in the Ubuntu 20.04 home folder. Make use of Ubuntu’s touch instruction for this purpose. We have been naming this file as “memcmp.c”. Now, it should be opened in some editor of Linux to create code within it i.e., vim, gnu Nano, and text editor.
We have been preferring the nano editor to open and create code within it. Both the commands have been shown in the screenshot below.
Let’s start our first example code for C to make use of the memcmp() function in the code. To start this code, we must require some C header files at the start of it to make our code work. Those libraries include “stdio.h” and “string.h” using the “#include” keyword. Overall working will be performed in the main() function for comparison of two string values.
An integer “n” has been declared. Two-character type arrays or string values “A” and “B” of size 10 have been initialized with string values. The values for both the character arrays are the same in length but different in-text i.e., capital and small letters of the same word.
The memcmp() function has been using the A and B strings within it to compare up to the first 5 letters of both strings. The return value would be saved to the “n” variable.
#include <string.h>
void main () {
int n;
char A[10] = "LINUX";
char B[10] = "linux";
n = memcmp(A, B, 5);
Here comes the nested if-else statement. The “if” part of the statement is checking whether the value of “n” is 0 or not. If so, will display that both values are equal in memory size using the “printf” function.
In the “else-if” part, it will check whether the value of “n” is greater than 0 or not. If so, it will display that the second string is smaller than the first string using the “printf” method. The else part will be shown that the first string is smaller than the second string if the value “n” is less than 0 utilizing the “printf” function. Let’s save our code to run it i.e., Ctrl+S.
printf("A is equal to B \n");
else if (n > 0)
printf("B is less than A \n");
else
printf("A is less than B \n");
return 0;
}
Do the compilation of this file code first with the gcc compiler along with the name of a file. After that, run the code file with the “./a.out” command shown in the attached image. It is presenting that string A is less than string B i.e., A is less in memory size.
Example 02:
Let’s take another similar example to see the working of memcmp function in C. This time, we have been using the capital letter string as A1 and the small letter string as A2 i.e., opposite to the first example.
The function memcmp() has been used to compare the memory size of the first 5 characters between the A1 and A2 variables. The return result has been saved to the integer “n”.
The if-else statement comes in handy here. The “if” part will be comparing of the return value of “n” is greater than 0 or not. If so, it will display the variable A string is greater than string A2 in memory size. Otherwise, if the “n” is less than 0. The else-if printf() function will get executed showing that string A is smaller in memory size than the string A2.
#include <string.h>
void main () {
int n;
char A1[10] = "Dreams";
char A2[10] = "DREAMS";
n = memcmp(A1, A2, 5);
if (n > 0)
printf("A1 is greater than to A2 \n");
else if (n < 0)
printf("A1 is less than A2 \n");
If both the above conditions don’t meet and the value of “n” is equal to “0”, the printf() method will display that the memory size of A1 and A2 is the same.
printf("A1 is equal to A2 \n");
return 0;
}
Let’s compile our C code before running it i.e., using the gcc compiler with the file name. After running the file, we have got the message that A1 is greater than A2 in memory size.
Example 03:
Let’s take a look at last but not the least examples in this article. There is one change in the overall code of this file. We have been using the same value for string variables A1 and A2 of the same size i.e., 10.
The values for both variable strings are showing the whole capital letters without any special character and space. The same memcmp() function is here to compare the memory size of A1 and A2. The First 5 letters for both strings will be compared. The overall same procedure has been applied in the code i.e., “if-else” statement with the same conditions.
#include <string.h>
void main () {
int n;
char A1[10] = "DREAMS";
char A2[10] = "DREAMS";
n = memcmp(A1, A2, 5);
if (n > 0)
printf("A1 is greater than to A2 \n");
else if (n < 0)
printf("A1 is less than A2 \n");
else
printf("A1 is equal to A2 \n");
return 0;
}
Compile and run your newly updated code. In return, we have got that the A1 and A2 strings are equal in memory size.
Conclusion
This article was all about the usage of memcmp function usage in the C programming language. We have compiled all the codes by using the gcc compiler in Ubuntu 20.04. We have put an immense amount of hard work to make this guide easy to understand for our C users.