Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

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. It means, instead of comparing the whole string, 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 used to compare user-specified string with existing string for n number of characters. This program will help you to understand the strncmp with multiple examples.

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

/* C strncmp Function example  */

#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

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;
}
strncmp in C language Example 2

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.

/* string strncmp in C programming Example */
 
#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, and see the result

strncmp in C language Example 4

Filed Under: C Programming

  • C Comments
  • C Escape Sequence
  • C Programming Operators
  • C Arithmetic Operators
  • C Assignment Operators
  • C Bitwise Operators
  • C Conditional Operator
  • C Increment & Decrement Optr
  • C Logical Operators
  • C Relational Operators
  • C Sizeof Operator
  • C If Statement
  • C IF ELSE Statement
  • C Else If Statement
  • C Nested If
  • C Break Statement
  • C Continue Statement
  • C Goto Statement
  • C Switch Case
  • C For Loop
  • C While Loop
  • C Do While Loop
  • C Arrays
  • C Two Dimensional Array
  • C Multi Dimensional Array
  • C String
  • C Structures
  • C Nested Structures
  • C Array of Structures
  • C Structures and Functions
  • C Union
  • C Structure & Union differences
  • C Pointers
  • C Pass Pointers to Functions
  • C GETS
  • C fgets Function
  • C fgetc
  • C fputs function
  • C fputc
  • C Functions
  • C Types of Functions
  • C Pass Array to Functions
  • Call By Value & Call By Reference
  • C Recursion
  • C Program Examples

Copyright © 2021· All Rights Reserved by Suresh.
About | Contact | Privacy Policy