strcmp Function in c
strcmp( ) in C programming language

If you looking for strcmp function in c programming read this blog post completely.

What is strcmp in C?

strcmp function is used to compare two strings. If both strings are identical it returns zero.

It is defined in string.h header file.

It takes two strings and returns an integer.

It compares two strings character by character. If the first character of two strings is equal it continues the process for the next characters. This process continues until it reaches null character or anyone character is different.

For example consider two strings as “abc”,”abc”. As both are identical it returns zero.

Prototype

strcmp prototype is as follows:

int strcmp(const char* string1,const char* string2);

Parameters for strcmp Function

strcmp takes two strings in order to compare them.

Return Value of strcmp Function

The strcmp return value is:

  • zero: If both strings are identical.
  • Negative: If ASCII value of first unmatched character is less than the second.
  • Positive: If ASCII value of first unmatched character is more than the second.

How to use strcmp in c?

In order to use strcmp function to compare two c strings, first include string header file like #include<string.h>.

Call strcmp function by passing two strings and store the return value is an int type variable, like int rv=strcmp(str1,str2);


strcmp( ) Example Programs

Example 1

#include<stdio.h>
#include<string.h>
int main()
{
  char strg1[]="abc",strg2[]="abc";
  int res;
  res=strcmp(strg1,strg2);
  printf("The comparison result is :%d \n",res);
  return 0;
} 
strcmp in cprogramming example 1
strcmp function programming example 1

Code explanation

The two input strings are passed to the strcmp function for comparison. The result is printed onto the console.


Example 2

#include<stdio.h>
#include<string.h>
int main()
{
  char strg1[]="abc",strg2[]="abB";
  int res;
  res=strcmp(strg1,strg2);
  printf("The comparison result is :%d \n",res);
  return 0;
} 
strcmp in c programming example 2
strcmp c programming example 2

Code explanation

The two input strings are passed to the strcmp function for comparison. The result is printed onto the console.


Difference between strlen and strcmp Functions in C Programming

strlen in c is used to calculate the length of a string. strcmp in c is used to compare two strings.

strlen returns total length of a string. strcmp returns

  • zero: If both strings are identical.
  • Negative: If ASCII value of first unmatched character is less than the second.
  • Positive: If ASCII value of first unmatched character is more than the second.

strlen terminates when it encounters a null character. strcmp terminates when one of the comparing characters is a different or null character.

General forms are as follows

int  strlen(const char*string);

int strcmp(const char* string1,const char* string2);
strlen in cstrcmp in c
Used to calculate the length of a string.Used to compare two strings.
int strlen(const char*strng);int strcmp(const char* string1,const char* string2);
Terminates when it encounters null character.Terminates when one of the comparing character is different or null character.

Difference between strcat and strcmp in C Programming

strcat in c appends source string to the destination string. strcmp in c is used to compare two strings.

strcat function returns the pointer to the destination string. strcmp returns zero, negative, positive values depending on the result.

strcat returns pointer to the string. strcmp returns integer value.

Prototypes are as follows

char *strcat(char *destination ,const char*source);

int strcmp(const char* string1,const char* string2);
strcat in cstrcmp in c
Appends source string to the destination string.Used to compare two strings.
char *strcat(char *destination ,const char*source);int strcmp(const char* string1,const char* string2);
Returns pointer to the destination string.Returns zero,negative,positive values depending on result.

Difference between strrev and strcmp in C Programming

strrev in c is used to reverse a given string. strcmp in c is used to compare two strings.

strrev returns a reversed string. strcmp returns zero, negative, positive values depending on result.

strrev takes one argument. strcmp takes two arguments.

strrev returns pointer to the string. strcmp returns integer value.

General forms are as follows


char *strrev(char * strin);

int strcmp(const char* string1,const char* string2);
strrev in cstrcmp in c
Used to reverse a given string.Used to compare two strings.

char *strrev(char * strin);
int strcmp(const char* string1,const char* string2);
Returns a reversed string.Returns zero, negative, positive values depending on the result.

Difference between strcmp and strcpy in C Programming

strcpy is used to copy the source string to destination string. strcmp in c is used to compare two strings.

strcpy returns pointer to the destination string. strcmp returns zero, negative, positive values depending on result.

strcpy returns pointer to the string. strcmp returns integer value.

General forms are as follows

char *strcpy(char *destination ,const char*source);

int strcmp(const char* string1,const char* string2);
strcmp in cstrcpy in c
Used to compare two strings.Used to copy the source string to destination string.
int strcmp(const char* string1,const char* string2);char *strcpy(char *destination ,const char*source);
Returns zero,negative,positive values depending on result.Returns pointer to the destination string.

People Also Ask

What is the difference between strncat and strcmp in c?

strncat appends source string to the destination string up to n characters. strcmp is used to compare two strings.

What is the difference between strchr and strcmp in c?

strchr is used to search for the first occurrence of a character in the pointed string. strcmp is used to compare two strings.

How do i use strcmp in c?

strcmp in c is used to compare two strings. It compares strings character by character using ASCII value of the characters.

Does strcmp check for null?

strcmp is used to compare two strings. If it encounters null it terminates the process.

What is the strcmp return value in c

strcmp function returns:
zero:- If both strings are identical.
Negative: If ASCII value of first unmatched character is less than the second.
Positive: If ASCII value of first unmatched character is more than the second.