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 in C Programming 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 C 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);
}
 The Comparison of str1 and str2 Strings = 0
 The Comparison of str1 and str2 Strings = 1
 The Comparison of str1 and str3 = -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

strncmp in C Example 2

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

/* C strncmp Function example  */

#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

strncmp Example 3

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

#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");
	}
	
}
strncmp in C language Example 3

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