In this tutorial, We will learn about strncmp string function in C programming language
strncmp() – In the c programming language, the strncmp() function returns a negative , Zero and positive integer depending on whether the first n character of the object pointed by str1 is less than, equal to, or greater than the first n character of the object pointed to by str2.(compare str1 and str2)
The syntax for strncmp() function is given below, in C Language is:
int strncmp(const char *str1, const char *str2,size of n);
str1 – this is the first string to be compared
str2 – this is the second string to be compared
n- the maximum number of character to be compared
the strncmp function returns an integer value as follows
when strncmp() returns value<0 then it denotes str1 is less than str2
when strncmp() returns value>0 then it denotes str1 is grater than than str2
when strncmp() returns value=0 then it denotes str1 is equal to str2
the required header for strncmp() String function in C Programming language
#include<string.h>
Example
Program 1
If both strings are same
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char str1[]="Hello"; char str2[]="Hello"; int check=strncmp(str1,str2,4); if(check<0){ printf("str1 is less than to str2\n"); } else if(check>0){ printf("str1 is greater than to str2\n"); } else{ printf("str1 is equal to str2\n"); } getch(); return 0; }
When the above code is executed, it produces the following results:
str1 is equal to str2
If both strings are not the same
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char str1[]="Hello"; char str2[]="hello"; int check=strncmp(str1,str2,4); if(check<0){ printf("str1 is less than to str2\n"); } else if(check>0){ printf("str1 is greater than to str2\n"); } else{ printf("str1 is equal to str2\n"); } getch(); return 0; }
When the above code is executed, it produces the following results:
str1 is less than to str2
There are other C programming language functions that are similar to this function
strcat() function in C language
strcpy() function in C language
strncpy() function in C language
strset() function in C language
strlwr() function in C language
strdub() function in C language
strupr() function in C language
strlen() function in C language
strnset() function in C language
strchr() function in C language
strrchr() function in C language
strtok() function in C language
strrev() function in C language
strcmp() function in C language
strnset() function in C language
Suggested for you
String function in C programming Language
Function in C programming language
Arithmetic function in C language
User defined function in C programming language
recursive Function in C language
recursive Function in C++ language
recursive Function in Python language
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…