w3resource

C - strncmp() function

C strncmp() function - compare part of two strings

Syntax:

int strncmp(const char *string1, const char *string2, size_t n)

The strncmp() function is used to compare string1 and string2 to the maximum of n.

Parameters:

Name Description Required /Optional
str1 This is the first string to be compared. Required
str2 This is the second string to be compared. Required
n The maximum number of characters to be compared. Required

Return value from strncmp()

This function return values that are as follows −

Name Value
string1 is less than string2 < 0
string1 is identical to string2 0
string1 is greater than string2 >0

Example: strncmp() function

#include <stdio.h>
#include <string.h>
#define SIZE 10
int main(void)
{
  int  result;
  int  index = 3;
  char string1[SIZE] = "pqruvxy";
  char string2[SIZE] = "pqrxy";
  void print_result( int, char *, char * );
  result = strncmp( string1, string2, index);
  printf( "\nComparison of only the first %i characters\n", index );
  printf( "  strncmp: " );
  print_result( result, string1, string2);
  index = 4;
  result = strncmp( string1, string2, index);
  printf( "\nComparison of only the first %i characters\n", index );
  printf( "  strncmp: " );
  print_result( result, string1, string2 );
}
void print_result( int res, char * p_buffer1, char * p_buffer2 )
{
  if ( res == 0 )
    printf( "\"%s\" is identical to \"%s\"\n", p_buffer1, p_buffer2);
  else if ( res < 0 )
    printf( "\"%s\" is less than \"%s\"\n", p_buffer1, p_buffer2 );
  else
    printf( "\"%s\" is greater than \"%s\"\n", p_buffer1, p_buffer2 );
}

Output:

Comparison of only the first 3 characters
  strncmp: "pqruvxy" is identical to "pqrxy"

Comparison of only the first 4 characters
  strncmp: "pqruvxy" is less than "pqrxy"

Example: strcmp() function and strncmp() function

Following example shows the difference between the strcmp() function and the strncmp() function.

#include <stdio.h>
#include <string.h>
 
#define SIZE 10
 
int main(void)
{
  int  result;
  int  index = 4;
  char string1[SIZE] = "pqrswxyz";
  char string2[SIZE] = "pqrstuvw";
  void test( int, char *, char * );
 
  result = strcmp( string1, string2 );
  printf( "Comparison of each character\n" );
  printf( "strcmp: " );
  test( result, string1, string2 );
 
  result = strncmp( string1, string2, index);
  printf( "\nComparison of only the first %i characters\n", index );
  printf( "strncmp: " );
  test( result, string1, string2 );
}
 
void test( int res, char * p_string1, char * p_string2 )
{
  if ( res == 0 )
    printf( "\"%s\" is identical to \"%s\"\n", p_string1, p_string2);
  else if ( res < 0 )
    printf( "\"%s\" is less than \"%s\"\n", p_string1, p_string2 );
  else
    printf( "\"%s\" is greater than \"%s\"\n", p_string1, p_string2 );
} 

Output:

Comparison of each character
strcmp: "pqrswxyz" is greater than "pqrstuvw"

Comparison of only the first 4 characters
strncmp: "pqrswxyz" is identical to "pqrstuvw"

C Programming Code Editor:

Previous C Programming: C strcmp()
Next C Programming: C strcoll()



Follow us on Facebook and Twitter for latest update.