std::strncmp() in C++

09 Jan, 2019
std::strncmp() function lexicographically compares not more than count characters from the two null-terminated strings and returns an integer based on the outcome.
This function takes two strings and a number num as arguments and compare at most first num bytes of both the strings.
num should be at most equal to the length of the longest string. If num is defined greater than the string length than comparison is done till the null-character(‘\0’) of either string.
This function compares the two strings lexicographically. It starts comparison from the first character of each string. If they are equal to each other, it continues and compare the next character of each string and so on.
This process of comparison stops until a terminating null-character of either string is reached or num characters of both the strings matches.
Syntax :
int strncmp(const char *str1, const char *str2, size_t count);

Parameters:
str1 and str2: C string to be compared.
count: Maximum number of characters to compare.
 size_t is an unsigned integral type.

Return Value: 
Value                               Meaning
Less than zero                      str1 is less than str2.
Zero                                str1 is equal to str2.
Greater than zero                   str1 is greater than str2.

If there are less than count characters in either string, the comparison ends when the first null is encountered.
What does strcmp() return?
strncmp() function return three different types of integer values on the basis of comparison:
1. Greater than zero ( >0 ): A positive value is returned, if a character of str1 and str2 doesn’t match before the num characters and the ASCII value of str1 character is greater than ASCII value of str2 character. As a result, we can say that str1 is lexicographically greater than str2.
// C, C++ program to demonstrate// functionality of strncmp()  #include <stdio.h>#include <string.h>  int main(){    // Take any two strings    char str1[10] = "aksh";    char str2[10] = "akash";      // Compare strings using strncmp()    int result = strncmp(str1, str2, 4);      if (result == 0) {        // num is the 3rd parameter of strncmp() function        printf("str1 is equal to str2 upto num characters\n");    }    else if (result > 0)        printf("str1 is greater than str2\n");    else        printf("str2 is greater than str1\n");      printf("Value returned by strncmp() is: %d", result);      return 0;}
Output:
str1 is greater than str2
Value returned by strncmp() is: 18

2. Less than zero ( <0 ): A negative value is returned, if a character of str1 and str2 doesn’t match before the num characters and the ASCII value of str1 character is lesser than ASCII value of str2 character. As a result, we can say that str2 is lexicographically greater than str1.
// C, C++ program to demonstrate// functionality of strncmp()  #include <stdio.h>#include <string.h>  int main(){    // Take any two strings    char str1[10] = "akash";    char str2[10] = "aksh";      // Compare strings using strncmp()    int result = strncmp(str1, str2, 4);      if (result == 0) {        // num is the 3rd parameter of strncmp() function        printf("str1 is equal to str2 upto num characters\n");    }    else if (result > 0)        printf("str1 is greater than str2\n");    else        printf("str2 is greater than str1\n");      printf("Value returned by strncmp() is: %d", result);      return 0;}
Output:
str2 is greater than str1
Value returned by strncmp() is: -18

3. Equal to zero ( 0 ): This function returns zero if the characters of str1 matches with the characters of the str2 upto num characters. As a result, we cannot say that str1 is equal to str2, until num is equal to length of either string.
// C, C++ program to demonstrate// functionality of strncmp()  #include <stdio.h>#include <string.h>  int main(){    // Take any two strings    char str1[10] = "akash";    char str2[10] = "akas";      // Compare strings using strncmp()    int result = strncmp(str1, str2, 4);      if (result == 0) {        // num is the 3rd parameter of strncmp() function        printf("str1 is equal to str2 upto num characters\n");    }    else if (result > 0)        printf("str1 is greater than str2\n");    else        printf("str2 is greater than str1\n");      printf("Value returned by strncmp() is: %d", result);      return 0;}
Output:
str1 is equal to str2 upto num characters
Value returned by strncmp() is: 0

Note: When the strings are not same, you will find that the value returned by the strncmp() function is the difference between the ASCII values of first unmatched character in str1 and str2 in both the cases.
More Examples
Example 1:
// CPP program to illustrate strncmp()#include <cstring>#include <iostream>  void display(char* abc, char* xyz, int res, int count){    if (res > 0)        std::cout << xyz << " come-before " << abc;    else if (res < 0)        std::cout << abc << " come-before " << xyz;    else        std::cout << "First " << count << " characters of string "         << abc << " and " << xyz << " are same";}  int main(){    char abc[] = "GeeksforGeeks";    char xyz[] = "Geeks";    int res;    res = std::strncmp(abc, xyz, 4);    display(abc, xyz, res, 4);    return 0;}
Output:
First 4 characters of string GeeksforGeeks and Geeks are same

Example 2:
// CPP program to illustrate strncmp()#include <cstring>#include <iostream>  void display(char* abc, char* xyz, int res, int count){    if (res > 0)        std::cout << xyz << " come-before " << abc;    else if (res < 0)        std::cout << abc << " come-before " << xyz;    else        std::cout << "First " << count << " characters of string " <<         abc << " and " << xyz << " are same";    ;}  int main(){    char abc[] = "GeeksforGeeks";    char xyz[] = "Geeks";    int res;    res = std::strncmp(abc, xyz, 6);    display(abc, xyz, res, 6);    return 0;}
Output:
Geeks come-before GeeksforGeeks

This article is contributed by Akash Gupta and Shivani Ghughtyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
cpp-strings-library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Bitwise Operators in C/C++
Set in C++ Standard Template Library (STL)
vector erase() and clear() in C++
unordered_map in C++ STL
Inheritance in C++
Priority Queue in C++ Standard Template Library (STL)
The C++ Standard Template Library (STL)
Substring in C++
Object Oriented Programming in C++
C++ Classes and Objects