C++ Standard Library C++ STL Library

C++ <cstring> - strncmp() Function



The C++ <cstring> strncmp() function is used to compare up to num characters of two null-terminated byte strings lexicographically. The function starts comparing the first pair of characters of each string and continues comparing until the pair of characters are different in specified number of comparisons or until a terminating null-character is reached.

Syntax

int strncmp ( const char * lhs, const char * rhs, size_t num );

Parameters

lhs Specify pointer to the first null-terminated byte string to compare.
rhs Specify pointer to the second null-terminated byte string to compare.
num Specify maximum number of characters to compare.
size_t is an unsigned integer type.

Return Value

Based on the value of lhs and rhs, the function returns the following:

  • Negative value if lhs appears before rhs in lexicographical order.
  • Zero if lhs and rhs compare equal.
  • Positive value if lhs appears after rhs in lexicographical order.

Example: Value returned by function = 0

The example below shows the usage of strncmp() function.

#include <iostream>
#include <cstring>
using namespace std;
 
int main (){
  char str1[20] = "Hello World!";
  char str2[20] = "Hello";
  
  //comparing first 5 characters 
  //of str1 and str2
  int retval = strncmp(str1, str2, 5);

  //displaying the result
  if(retval != 0)
    cout<<"First 5 characters of str1 and str2 are not equal.\n";
  else
    cout<<"First 5 characters of str1 and str2 are equal.\n";
 
  cout<<"Value returned by the function: "<<retval<<"\n";

  return 0;
}

The output of the above code will be:

First 5 characters of str1 and str2 are equal.
Value returned by the function: 0

Example: Value returned by function < 0

Lets consider this example where the returned value is less than zero.

#include <iostream>
#include <cstring>
using namespace std;
 
int main (){
  char str1[20] = "Hello";
  char str2[20] = "World";
  
  //comparing first 3 characters 
  //of str1 and str2
  int retval = strncmp(str1, str2, 3);

  //displaying the result
  if(retval != 0)
    cout<<"First 3 characters of str1 and str2 are not equal.\n";
  else
    cout<<"First 3 characters of str1 and str2 are equal.\n";

  cout<<"Value returned by the function: "<<retval<<"\n";

  return 0;
}

The output of the above code will be:

First 3 characters of str1 and str2 are not equal.
Value returned by the function: -15

Example: Value returned by function > 0

Lets consider another example where the returned value is greater than zero.

#include <iostream>
#include <cstring>
using namespace std;
 
int main (){
  char str1[20] = "Hello";
  char str2[20] = "Apple";
  
  //comparing first 3 characters 
  //of str1 and str2
  int retval = strncmp(str1, str2, 3);

  //displaying the result
  if(retval != 0)
    cout<<"First 3 characters of str1 and str2 are not equal.\n";
  else
    cout<<"First 3 characters of str1 and str2 are equal.\n";

  cout<<"Value returned by the function: "<<retval<<"\n";

  return 0;
}

The output of the above code will be:

First 3 characters of str1 and str2 are not equal.
Value returned by the function: 7

❮ C++ <cstring> Library