C language strcmp () function and strncmp () function

strcmp() function

<string.h>

int strcmp(const char *str1, const char *str2)

parameter

str1 – the first string to compare.

str2 – the second string to compare.

Compares the string pointed to by str1 with the string pointed to by str2.

return value

The return value of this function is as follows:

If the return value is less than 0, it means that str1 is less than str2.

If the return value is greater than 0, it means that str1 is greater than str2.

If the return value is equal to 0, it means that str1 is equal to str2.

The ANSI standard says that strcmp() returns a negative number if the first string comes before the second alphabetically, returns 0 if they are the same, and returns a positive number if the first string follows the second alphabetically. The first string comes alphabetically before the second, strcmp() returns a negative number, or 0 if they are the same, and if the first string comes alphabetically after the second then returns a positive number.)

strncmp() function

<string.h>

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

parameter

str1 – the first string to compare.

str2 – the second string to compare.

n – The maximum number of characters to compare.

Compare str1 and str2 up to the first n bytes.

return value

The return value of this function is as follows:

If the return value < 0, it means that str1 is smaller than str2.

If the return value > 0, it means that str1 is greater than str2.

If the return value = 0, it means that str1 is equal to str2.

Guess you like

Origin blog.csdn.net/chengkai730/article/details/132397524