Compares up to a maximum number of characters.
#include <string.h> int strncmp(const char *s1, const char *s2, size_t n);
s1
A pointer to the character string to compare.
s2
A pointer to the comparison string.
n
The maximum number of characters to compare.
This function compares n characters of the character array pointed to by s1 to n characters of the character array pointed to by s2. Neither s1 nor s2 needs to be nullterminated character arrays.
The function stops if it reaches a null character before n characters have been compared. The function returns:
This facility may not be available on some configurations of the EWL.
#include <string.h> #include <stdio.h> int main(void) { char s1[] = "12345anchor"; char s2[] = "12345zebra"; if (strncmp(s1, s2, 5) == 0) printf("%s is equal to %s\n", s1, s2); else printf("%s is not equal to %s\n", s1, s2); return 0; } Output: 12345anchor is equal to 12345zebra