The standard library provides many useful functions to ease the job of software engineers. Usually, the standard library provides library functions that have multiple variants which could be leveraged as per the requirement of the user of those library. For example, the standard C/C++ library function string( C-style) comparison has two variants i.e. strcmp and strncmp. This library function is present in string.h for C and cstring in C++. Looking at the operation performed by both variants of a string compare function, it does not matter which one of the string compare function used as the result remain the same in most cases. In fact, in many generic scenarios, it's true. But a closer look at strncmp reveal it's a better alternative to strcmp. Especially for defensive style of coding. Let's have a quick look at both the functions.
int strncmp ( const char * str1, const char * str2, size_t num );
strncmp compares up to num characters of the C string str1 to those of the C string str2.
This function starts by comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ, until a terminating null-character is reached, or until num characters match in both strings, whichever happens first.
int strcmp ( const char * str1, const char * str2, size_t num );
strcmp compares the C string str1 to the C string str2.
This function starts by comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
Both the function return value less than or greater than 0 depending upon whether str1 comes lexicographically before str2 or after and 0 if both strings are equal.
Now we know what both string compare functions are supposed to do. Let's try to understand how strncmp is better alternative to strcmp.
The major problem with strcmp is that if by mistake one of the arguments passed are not C-string i.e. str1 or str2 is not terminated with the NULL character '\0', then strcmp continues comparing until it reaches non-accessible memory and crashes or sometimes results in undefined behavior.
Using strncmp you can limit the search so that it does not reach non-accessible memory. This is especially useful if the char array contains some data which may not necessarily be string data. These types of C-strings are extensively used in the embedded systems where char array may contain addresses or data packets from some serial communication protocol. Another advantage of having a search limit is it allows us to compare sub-strings which is not possible with the strcmp function. Let's try to understand this with the help of below simple example.
/* strncmp example to compare to string of different length */
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
const char *inputStr = "LiternalString_XYZ";
int cmp = strncmp(inputStr, "LiternalString", strlen("LiternalString"));
if(cmp==0)
cout<<"Sub string is present in the input string\n";
else
cout<<"Sub string is absent in the input string\n";
return 0;
}
Output of above code is:
Sub string is present in the input string