C strcmp( ) function

  • By using strcmp() function, we can compare one string wiht another string.
  • When we are working with strcmp() function, it requires two arguments of type const char* i.e. address string.
  • When we are working with strcmp() function, character by character comparision will takes place until first unpair character occurred, when unpair is occurred then it return ASCII value difference.
  • In strcmp() function, if both strings contains same data then it returns zero.

Syntax:

int strcmp(const char* s1, const char* s2);

Example:

#include
#include
#include
int main()
{
char s1[15]=”K2schools”;
char s2[12]=”K2s”;
int d;
clrscr();
puts(s1);
puts(s2);
d=strcmp(s1,s2);
printf(“ASCII value difference %d”,d);
getch();
return 0;
}

Powered by k2schools