strcmp function-Compare strings

The strcmp function is a function that compares strings. Returns a positive number if "s1" is greater than "s2" in lexicographical order, 0 if they are the same, and a negative number if they are smaller.

You can use it by including " string.h".

#include <string.h>
int strcmp (const char * s1, const char * s2);

There is a promise that C string ends with "\ 0". The strcmp function assumes this convention and compares strings.

Strcmp function sample

This is a sample that compares strings with the strcmp function. If there is a match, Match is displayed.

#include <stdio.h>
#include <string.h>

int main (void) {
  const char * message = "Hello";
  
  // Compare strings
  if (strcmp (message, "Hello") == 0) {
    printf("Match\n");
  }
  else {
    printf("Not Match\n");
  }
}

Associated Information