strncmp string function in C programming language
- Home
- function in C
- strncmp string function in C programming language
- On
- By
- 0 Comment
- Categories: function in C, Pre-define String function in C
strncmp string function in C programming language
strncmp string function in C programming language
In this tutorial, We will learn about strncmp string function in C programming language
Description
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)
Declaration
The syntax for strncmp() function is given below, in C Language is:
int strncmp(const char *str1, const char *str2,size of n);
Parameters
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
Returns
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
Required Header
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
Suggested for you
String function in C Language
String handling in C language
Arithmetic function in C language