strcoll in C language

The C strcoll function is a String Function, used to compare two strings. The result of this function depends upon the LC_COLLATE settings. The strcoll method returns any of the following values:

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

C strcoll syntax

The basic syntax of the strcoll in C Programming language is as shown below.

char *strcoll(const char *str1, const char *str2);

or we can simply write it as:

strcoll(str1, str2);

strcoll in C language Example

The strcoll function used to compare two strings based on the location settings. This program will help you to understand the strcoll with multiple examples.

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

/* C strcoll Function example  */

#include <stdio.h> 
#include<string.h>
 
int main()
{
   char str1[50] = "abcdef";
   char str2[50] = "abcdefgh";
   char str3[] =  "ghijk";
   char str4[] =  "GHIJK";
   int i, j, k;
	
   i = strcoll(str1, str2);		
   printf("\n The Comparison of str1 and str2 Strings = %d", i);
 	
   j = strcoll(str3, str4);		
   printf("\n The Comparison of str3 and str4 Strings = %d", j);
   
   k = strcoll(str1, "abcdef");		
   printf("\n The Comparison of both Strings = %d", k);
}
strcoll in C language Example 1

String strcoll Example 2

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

/* C strcoll Function example  */

#include <stdio.h> 
#include<string.h>
 
int main()
{
   	char str1[50] = "abcdefgh";
   	char str2[50] = "ABCDEFGH";
   	int result;
	
   	result = strcoll(str1, str2);
   
   	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

strcoll Function Example 3

This C Program allows the user to enter two strings. Next, it is going to compare those two strings using strcoll function.

/* C strcoll Function 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 = strcoll(str1, str2);
	
 	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");
	}
}
 Please Enter First String  : abcd

 Please Enter the String that you want to Compare : aBCD

 Second String is Less than First String

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.