strncmp in C


Reading time: 30 minutes | Coding time: 10 minutes

strncmp is a function in C which is used to compare two array of characters upon N indexes and return if the first array is less, equal or greater than the second array. It overcomes the limitations of strcmp function by allowing programmers to set the number of characters to compare.

To use strncmp function, we need to include string.h header file like:

#include <string.h>

Usage:

strncmp(array1, array2, index);

This will compare array1 and array2 for the first "index" elements. If the lenght of array1 or array2 (that is presence of null character \0) is less than "index", then it will stop before at the end of the smaller string.

It will return an integer as:

  • Any negative integer if array1 < array2
  • 0 if array1 = array2
  • Any positive integer if array1 > array2

The magnitude of the return value is actually the different of the ASCII value of the first different character.

Complete example:

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

int main() 
{
	char* data1 = "this is data1";
	char* data2 = "this is data1";
	printf("%d", strncmp(data1, data2, 6));
	return 0;
}

Output:

0

Consider this example where we are comparing two same array with n set within limits:

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

int main() 
{
	char data1[] = {'o', 'p', 'e', 'n', 'g', 'e', 'n', 'u', 's'};
	char data2[] = {'o', 'p', 'e', 'n', 'g', 'e', 'n', 'u', 's'};
	printf("%d", strncmp(data1, data2, 5));
	return 0;
}

Output:

0

If we set n out of limit that is more than 9 in this case ("opengenus" 9 characters), then it will compare garbage values and the output can be anything. Hence, it is the responsibility of the programmer to set the value of n correctly.

Consider this example:

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

int main() 
{
	char data1[] = {'o', 'p', 'e', 'n', 'g', 'e', 'n', 'u', 's'};
	char data2[] = {'o', 'p', 'e', 'n', 'g', 'e', 'n', 'u', 's'};
	printf("%d", strncmp(data1, data2, 11));
	return 0;
}

Output:

-5

Even through the data is same, it is showing that data1 is less than data2 as we have set the value of n incorrectly.

Can we compare null value \0 ?

The short answer is comparison terminates when the null character is encountered irrespective of the value of n in strncmp.

Consider this example in which both strings are same with a null character in between:

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

int main() 
{
	char data1[] = {'o', 'p', 'e', 'n', '\0', 'g', 'e', 'n', 'u', 's'};
	char data2[] = {'o', 'p', 'e', 'n', '\0', 'g', 'e', 'n', 'u', 's'};
	printf("%d", strncmp(data1, data2, 10));
	return 0;
}

Output:

0

As both strings are same, the output will be same but only the characters before the null characters are being considered.

Let us consider this example where the 4th character is different:

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

int main() 
{
	char data1[] = {'o', 'p', 'e', 'n', '\0', 'g', 'e', 'n', 'u', 's'};
	char data2[] = {'o', 'p', 'e', 'p', '\0', 'g', 'e', 'n', 'o', 's'};
	printf("%d", strncmp(data1, data2, 10));
	return 0;
}

Output:

-2

As expected, the strncmp works correctly.

Consider this example where the 7th character is different but strncpy is unable to detect it as there is a null character before it where the comparison stops.

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

int main() 
{
	char data1[] = {'o', 'p', 'e', 'n', '\0', 'g', 'e', 'n', 'u', 's'};
	char data2[] = {'o', 'p', 'e', 'n', '\0', 'g', 'o', 'n', 'o', 's'};
	printf("%d", strncmp(data1, data2, 10));
	return 0;
}

Output:

0

Hence, if you want to compare beyond null values, then use your own implementation.

The advantage of using strncmp is that there is no buffer overflow if the value of N (the third parameter) is correct. If value of n goes beyond the array length, it overflows to that character as well.

Consider this example where both strings are same but the value of n is 20 which is wrong as it should be within 9.

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

int main() 
{
	char data1[] = {'o', 'p', 'e', 'n', 'g', 'e', 'n', 'u', 's'};
	char data2[] = {'o', 'p', 'e', 'n', 'g', 'e', 'n', 'u', 's'};
	printf("%d", strncmp(data1, data2, 20));
	return 0;
}

Output:

-5

Hence, even when we are using strncmp, we need to pass the last parameter correctly.

With this, you have the complete knowledge of working with strncmp. Enjoy.