strncmp function-Compare substrings

The strncmp function is a function that partially compares strings. Returns a positive number if the specified number of characters "n" and "s1" is greater than "s2" in dictionary 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 strncmp (const char * s1, const char * s2, size_t n);

Sample strncmp function

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

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

int main (void) {
  const char * module_name = "Foo::Bar::Baz";
  
  // Find out if it starts with Foo::Bar
  if (strncmp (module_name, "Foo::Bar", strlen ("Foo::Bar")) == 0) {
    printf("Match\n");
  }
  else {
    printf("Not Match\n");
  }
}

How to compare whole strings?

Use the strcmp function to compare the entire string.

Associated Information