English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Online Tools

O)

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Flow Control

C Language Structure

C Language File

C Others

C Language Reference Manual

C Standard Library - <string.h>

Usage and example of the C library function strcmp() int strcmp(const char *str1, const char *str2) The following is the C library function str1 The string pointed to by the argument and str2 The string pointed to by the argument is compared.

Declaration

Below is the declaration of the strcmp() function.

int strcmp(const char *str1, const char *str2)

Parameters

  • str1 -- The first string to be compared.
  • str2 -- The second string to be compared.

Return Value

The return value of this function is as follows:

  • If the return value is less than 0, it means that the str1 Less than str2.
  • If the return value is greater than 0, it means that the str1 Greater than str2.
  • If the return value is equal to 0, it means that the str1 Equal to str2.

Online Example

The following example demonstrates the usage of the strcmp() function.

#include <stdio.h>
#include <string.h>
int main ()
{
   char str1[15];
   char str2[15];
   int ret;
   strcpy(str1, "abcdef");
   strcpy(str2, "ABCDEF");
   ret = strcmp(str1, str2);
   if (ret < 0)
   {
      printf("str1 Less than str2");
   {}
   else if(ret > 0) 
   {
      printf("str1 Greater than str2");
   {}
   else 
   {
      printf("str1 Equal to str2");
   {}
   return(0);
{}

Let's compile and run the above program, which will produce the following result:

str1 Greater than str2

C Standard Library - <string.h>