strcmp in c
strcmp( ) in C programming language

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

What is strcmp in c?

strcmp in c 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.

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

Prototype

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

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.

Parameters for strcmp in c

strcmp takes two strings in order to compare them.

Return value of strcmp in c

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.

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 in cprogramming 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 in 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 in c

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

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

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 result.

Difference between strcmp and strcpy in c

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 for

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.