Pointer to the first null-terminated string to compare.
2nd Parameter Type : const char *
Pointer to read-only string
2nd Parameter
Pointer to the second null-terminated string to compare.
3rd Parameter Type : size_t
Platform-specific unsigned type for array indices and memory sizes.
3rd Parameter
The maximum number of characters to compare from each string.
Read more about parameters of strncmp in parameters section
The strncmpfunction in C language Compares a specified number of characters from two strings.
The strncmp function compares up to n characters of the C string str1 to those of the C string str2. This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ, until a terminating null-character is reached, or until n characters match in both strings, whichever happens first.
The strncmpfunction takes 3
parameters:
•
const char * `s1`: Pointer to the first null-terminated string to compare.
•
const char * `s2`: Pointer to the second null-terminated string to compare.
•
size_t `n`: The maximum number of characters to compare from each string.
Compares up to `n` characters of two null-terminated strings `s1` and `s2` lexicographically. Returns a value less than, equal to, or greater than zero if `s1` is found to be less than, equal to, or greater than `s2`, respectively.
The strncmp function return value :
Returns an integer less than, equal to, or greater than zero if the first n bytes of str1 is found, respectively, to be less than, to match, or be greater than the first n bytes of str2
Output
This example demonstrates basic usage of strncmp to compare the first 7 characters of two strings.