What is strncmp() function in C Programming ?

strncmp function of C works same as strcmp( ) function which is used in C program for comparing two string together with one extra condition...

strncmp() function in c
strcmp() in C
Through this article we are going to discuss some important points which are mention below

  1. strncmp( ) function in C
  2. Header file for strncmp( ) in C
  3. Syntax of strncmp( ) function in C
  4. Return value of strncmp( ) function in C
  5. Example of strncmp( ) function in C

strncmp( ) function in C

strncmp( ) function is also known as a built in function or standard library function of C but it is widely know as string function. strncmp( ) function is basically used to compare two string assertion(argument) character by character by using 'n' parameters that means strncmp function compare both strings until n parameters.
strncmp( ) function is also compare two string together on the basis of ASCII value of each character, which are already define in C compiler.


Return value of strcmp( ) function in C

strncmp( ) function may be return three possible integer value based on the comparison...
  1. Zero( 0 ) : This value is return when ASCII value of both strings are equal.
  2. Greater than Zero( >o) : When the ASCII value of first string is greater than second string.
  3. Less than Zero( <0) : When the ASCII value of second string is greater than first string.

Header file of strncmp( ) function in C

strncmp( ) function is a built in function of C and it is define in <string.h> header file.

syntax of strncmp( ) function in C

strncmp( str1,str2,n);

Example of strncmp( ) function in C

#include<stdio.h>
#include<string.h>
int main()
{
  int n;
  char str[15]="Akash";
  char str1[15]="Ashish";
  n=strncmp(str,str1,1);
  if(n>0)
   {
     printf("str is greater than str1");
   }
   else if(n<0)
   {
     printf("str is less than str1");
   }
    else
   {
    printf("both string are equal");
   }
return 0;
}

____________________________________

example of strncmp
Strncmp( ) Example
____________________________________

Conclusion

This function is basically used to compare to string together by using 'n' parameters.
Post a Comment (0)
Previous Post Next Post