C programming string.h function - int strncmp(const char *str1, const char *str2, size_t n)

htt‮sp‬://www.theitroad.com

The strncmp function in the string.h library is used to compare the first n characters of two strings. It returns an integer that indicates the result of the comparison.

The function has the following signature:

int strncmp(const char *str1, const char *str2, size_t n);

where:

  • str1 - the first string to be compared
  • str2 - the second string to be compared
  • n - the maximum number of characters to compare

The strncmp function compares the first n characters of str1 and str2 and returns an integer that indicates the result of the comparison. The possible return values are:

  • If str1 is less than str2, the function returns a negative integer.
  • If str1 is greater than str2, the function returns a positive integer.
  • If str1 is equal to str2, the function returns 0.

Note that the comparison is case-sensitive. If you need a case-insensitive comparison, you can use the strncasecmp function instead.