The strncmp function compares not more than n characters (characters that follow a null character are not compared) from the array pointed to by s1 to the array pointed to by s2.
Syntax of strncmp():
int strncmp(const char *s1, const char *s2, size_t n);
Parameters:
s1 − This is the first string to be compared.
s2 − This is the second string to be compared.
n − The maximum number of characters to be compared.
Return:
Zero <= If s1 and s2 equal.
Negative value <= The first character that does not match has a lower value in s1 than in s2.
Positive value <= The first character that does not match has a greater value in s1 than in s2.
Sometimes peoples require to create their own strncmp function for their project. So here in the below section, I shall describe the way to create the strncmp() function or you can say that we will see the implementation of strncmp in C.
Let’s see an example code to understand the functionality of the strncmp in C. In this C code, we will compare two given string.
#include <stdio.h> #include <string.h> #define SIZE 15 int main () { int ret = 0; char s1[SIZE] = "Hello"; char s2[SIZE] = "Aticleworld"; ret = strncmp(s1, s2, 5); if(ret > 0) { printf("s2 is less than s1"); } else if(ret < 0) { printf("s1 is less than s2"); } else { printf("s1 is equal to s2"); } return 0; }
Output:
s2 is less than s1
Explanation: “Hello” is greater than “Aticleworld” because the first non-matching character in both words is ‘H’ and ‘A’ respectively, and ‘H’ (72) evaluates as greater than ‘A’ (65).
Some important points related to strncmp in C:
1.) It takes three parameters and you must include string.h header file in your C program.
2.) strncmp function compares the two strings lexicographically. It compares the two strings character by character starting from the first character until the characters in both strings are equal or a null character is encountered.
3.) strncmp function does not compare more than n characters of given strings s1 and s2.
4.) If characters of both string s1 and s2 are identical till n characters, then strncmp() function return 0. Let’s see an example to understand the concept.
#include <stdio.h> #include <string.h> int main () { int ret = 0; char s1[] = "Aticleworld"; char s2[] = "Atic"; //calling strncmp to compare s1 and s2 ret = strncmp(s1, s2, 3); if(ret > 0) { printf("s1 character greater than s2\n"); } else if(ret < 0) { printf("s1 character less than s2\n"); } else { printf("3 characters of strings s1 and s2 equal\n"); } return 0; }
Output:
3 characters of strings s1 and s2 equal
5.) When the first not matching character in left-string (s1) has a lesser ASCII value than the corresponding character in right-string(s2), then it returns a value lesser than zero.
#include <stdio.h> #include <string.h> int main () { int ret = 0; char s1[] = "Aticleworld"; char s2[] = "world"; //calling strncmp to compare s1 and s2 ret = strncmp(s1, s2, 3); if(ret > 0) { printf("s1 character greater than s2\n"); } else if(ret < 0) { printf("s1 character less than s2\n"); } else { printf("3 characters of strings s1 and s2 equal\n"); } return 0; }
Output:
s1 character less than s2
6.) When the first not matching character in left-string (s1) has a greater ASCII value than the corresponding character in right-string(s2), then it returns a value greater than zero.
#include <stdio.h> #include <string.h> int main () { int ret = 0; char s1[] = "world"; char s2[] = "Aticleworld"; //calling strncmp to compare s1 and s2 ret = strncmp(s1, s2, 3); if(ret > 0) { printf("s1 character greater than s2\n"); } else if(ret < 0) { printf("s1 character less than s2\n"); } else { printf("3 characters of strings s1 and s2 equal\n"); } return 0; }
Output:
s1 character greater than s2
Note: C standard only explains that the return value of strncmp function returns an integer greater than, equal to, or less than zero, accordingly, as the possibly null-terminated array, pointed to by s1 is greater than, equal to, or less than the possibly null-terminated array pointed to by s2.
7.)The behavior is undefined when access occurs past the end of either array s1 or s2.
8.)The behavior is undefined when either s1 or s2 is the null pointer.
Difference between memcmp, strcmp, and strncmp in C:
You can see below C program for better understanding,
#include <stdio.h> #include <string.h> int main () { const char s1[] = "ABCDE\0\0\0\0"; // append extra null bytes at end const char s2[] = "ABCDE\0abc"; // embedded null byte const char s3[] = "ABCDEFGH"; /*1*/ printf("strcmp(s1, s2) = %d\n",strcmp(s1, s2)); /*2*/ printf("strcmp(s1, s3) = %d\n",strcmp(s1, s3)); /*3*/ printf("strncmp(s1, s3, 5) = %d\n",strncmp(s1, s3, 5)); /*4*/ printf("memcmp(s1, s3, 5) = %d\n",memcmp(s1, s3, 5)); /*5*/ printf("strncmp(s1, s2, 8) = %d\n",strncmp(s1, s2, 8)); /*6*/ printf("memcmp(s1, s2, 8) = %d\n",memcmp(s1, s2, 8)); return 0; }
Output:
Recommended Articles for you:
- Use of strcmp in C.
- How to use memcmp in C?
- Implement and use of memset of in C
- How to make memcpy function in C
- Implement own memmove in C.
- memmove vs memcpy.
- Implement vector in C.
- How to Use strncpy() and implement own strncpy().
- Implement your own strcat in C.
- How to pass an array as a parameter?
- Implement own atoi in C.
- 10 Best C Programming Books.
- Best mouse for a programmer.
- Pointer Arithmetic in C.
- void pointer in C.
- A brief description of the pointer in C.
- Dangling, Void, Null and Wild Pointers
- When and how to use array in C?
- Memory Layout in C.
- File handling in C, In a few hours.