strncmp in C language

The C strncmp function is a String Function used to compare two strings. Or it checks whether those two strings are equal or not. The strncmp function uses the third argument to limit the comparison. Instead of comparing the whole string, it means you can compare the first four characters, or five characters, etc. The strncmp method returns any of the following three values:

  • It will return -1 if str1 is less than str2
  • returns +1 if str1 is greater than str2
  • The function will return 0 if str1 and str2 are equal

C strncmp syntax

The basic syntax of the strncmp in this Programming language is as shown below.

char *strncmp(char *str1, char *str2, size_t n);

or we can simply write this strncmp is as shown below:

strncmp(str1, str2, string_length);
  • n: The maximum number of characters that you want to compare.

strncmp in C language Example

The strncmp function is used to compare a user-specified string with an existing string for n number of characters. This program will help you to understand the strncmp with multiple examples.

TIP: You must include the #include<string.h> header before using this strncmp String Function.

#include <stdio.h> 
#include<string.h>
 
int main()
{
   char str1[50] = "abcdef";
   char str2[50] = "abcd";
   char str3[] =  "ghi";
   int i, j, k;
	
   i = strncmp(str1, str2, 4);		
   printf("\n The Comparison of str1 and str2 Strings = %d", i);
 	
   j = strncmp(str1, str2, 6);		
   printf("\n The Comparison of str1 and str2 Strings = %d", j);
   
   k = strncmp(str1, str3, 3);		
   printf("\n The Comparison of str1 and str3 = %d", k);
}
strncmp in C language Example 1

It compares the first four characters because the third argument is 4.

i = strncmp(str1, str2, 4); // abcd = abcd

This C Programming statement will compare the first six characters because the third argument is 6.

j = strncmp(str1, str2, 6); // abcdef = abcd

Using C strncmp function with else if condition

Instead of printing 0, 1, and -1 as a result, this strncmp program will print a meaningful message using the Else If Statement.

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

int main()
{
char str1[50] = "abcdefgh";
char str2[50] = "ABC";
int result;

result = strncmp(str1, str2, 3);

if(result < 0)
{
printf("\n str1 is Less than str2");
}
else if(result > 0)
{
printf("\n str2 is Less than str1");
}
else
{
printf("\n str1 is Equal to str2");
}
return 0;
}
 str2 is Less than str1

How to compare the first N characters of two strings in C?

This program allows the user to enter two strings. Next, it is going to compare those two strings using strncmp function.

#include <stdio.h> 
#include<string.h>
 
int main()
{
	char str1[100], str2[100];
	int result;
 
	printf("\n Please Enter First String  : ");
	gets(str1);	
	
	printf("\n Please Enter the String that you want to Compare : ");
	gets(str2);	
	
	result = strncmp(str1, str2, 4);
	
 	if(result < 0)
   	{
   		printf("\n First String is Less than Second String");
	}
	else if(result > 0)
   	{
   		printf("\n Second String is Less than First String");
	}
	else
   	{
   		printf("\n First String is Equal to Second String");
	}
	
}
C strncmp Example 2

Let me change the Size value from 4 to 12 and see the result.

 Please Enter First String  : Good Morning

 Please Enter the String that you want to Compare : Good Afternoon

 Second String is Less than First String

What does strncmp return if the strings match?

The strncmp returns 0 when the given string matches.

#include <stdio.h>
#include <string.h>
int main()
{
printf("%d", strncmp("Hello", "Hello", 5));
}
0

Why should I use C strncmp instead of strcmp?

Both strncmp() and strcmp() functions perform string comparisons.

  • strncmp() compares the first N characters.
  • strcmp() compares a whole string until it finds the null terminator.

If your goal is to check whether the string starts with a certain substring or compare a fixed number of characters, strncmp() is the best choice. When you are looking for an exact match of both strings, use strcmp(). For instance, when validating a username, we must prefer the strcmp().

In the following program, the strcmp() compares “abc” against “abcdef” as the first string is less than the second, the program returns a negative value. On the other hand, the strncmp() compares the first 3 characters from the source against the destination. So, “abc” compares with “abc” and the result is 0.

#include <stdio.h>
#include <string.h>
int main()
{
char s1[50] = "abc";
char s2[50] = "abcdef";
int r1 = strcmp(s1, s2);
int r2 = strncmp(s1, s2, 3);
printf("%d\n", r1);
printf("%d", r2);
}
-1
0

Does strncmp stop at the null terminator \0?

Yes. The strncmp() function stops when it finds any difference in both strings, after comparing the n characters, or it encounters the null terminator \0.

C strncmp causes a segmentation fault or undefined behaviour

When trying to compare strings (character arrays) without a null terminator, the strncmp() causes a segmentation fault. In the following example, the first strncmp() statement compares the first three characters, so it returns 0 as output. However, the other statement tries to compare strings without a null terminator, which may cause a segmentation fault.

#include <stdio.h>
#include <string.h>
int main()
{
char s1[3] = {'a', 'b', 'c'};
char s2[3] = {'a', 'b', 'c'};
int r1 = strncmp(s1, s2, 3);
int r2 = strncmp(s1, s2, 5);
printf("%d\n", r1);
printf("%d", r2);
}

Similarly, when we use a NULL value or NULL pointer as one of the strncmp() function arguments, it causes undefined behavior.

#include <stdio.h>
#include <string.h>
int main()
{
char s1[5] = "abc";
char *s2 = NULL;
int r = strncmp(s1, s2, 3);
printf("%d\n", r);
}
Segmentation Fault