A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/c_standard_library/c_function_strncmp.htm below:

C Library - strncmp() function

C Library - strncmp() function

The C library strncmp() function is used to compare at most a specified number of characters from two null-terminated strings. This string is also known as end of the string i.e. defined through the occurrence of a null character.

Syntax

Following is the syntax of the C library strncmp() function −

strncmp(const char *str1, const char *str2, size_t n)
Parameters

This function accepts the following parameters −

Return Value

This function returns integer value which is −

Example 1

Following is the C library strncmp() function that checks whether the given two strings are equal or not.

#include <stdio.h> 
#include <string.h>  
int main() 
{  
   char str_1[] = "java";  
   char str_2[] = "java";      
   if (strncmp(str_1, str_2, strlen(str_2)) == 0) 
   {  
      printf("The strings '%s' and '%s' are equal.\n", str_1, str_2);  
   }  
   else 
   {  
      printf("The strings '%s' and '%s' are not equal.\n", str_1, str_2);  
   }  
   return 0;  
}  

Output

On execution of above code, we get the following result −

The strings 'java' and 'java' are equal.
Example 2

Below the mentioned program state the two functions of C − strcpy() which create the string copy and using strncmp(), it compares the string.

#include <stdio.h>
#include <string.h>

int main () 
{
   char str1[15];
   char str2[15];
   int ret;
   strcpy(str1, "abcdef");
   strcpy(str2, "abcdEF");

   ret = strncmp(str1, str2, 4);
   if(ret < 0) 
   {
      printf("str1 is less than str2");
   } 
   else if(ret > 0) 
   {
      printf("str2 is less than str1");
   } 
   else 
   {
      printf("str1 is equal to str2");
   }
   return(0);
}
Output

The above code produces the following output−

str2 is less than str1

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4