What does strncmp actually do?
strncmp compares the first two characters in the strings. (When comparing characters, it uses their values as unsigned char, even though they are passed via pointers to char.) If the characters differ or either is a null character:
- If the character from the first string is greater than the character from the second,
strncmpreturns a positive value. - If the first is less than the second,
strncmpreturns a negative value. - Otherwise (the characters are both null characters),
strncmpreturns zero.
If the characters did not differ and neither was a null character, strncmp goes on to compare the following characters in the same way, until n pairs have been compared. If no difference has been found after n pairs have been compared, strncmp returns zero.
Some implementations of strncmp may return the signed difference between the two characters that differed, but this is not required by the C standard. strncmp may simply return +1 and −1 for “greater than” and “less than” or may use other positive and negative values.
Is strcmp in embedded C safe? If not, what are the usual substitutes?
Strcmp function doesn’t work healthy with strings includes blank. How can I handle it? For example when I compare “Hello World” and “Hello” the function’s value is not 0 or 1. I think it is about ‘\0’ but I don’t know how to handle it
What's with all the casting to unsigned char in strcmp.c?
Implementing my own strcmp - have I got it right?
Videos
What does strncmp actually do?
strncmp compares the first two characters in the strings. (When comparing characters, it uses their values as unsigned char, even though they are passed via pointers to char.) If the characters differ or either is a null character:
- If the character from the first string is greater than the character from the second,
strncmpreturns a positive value. - If the first is less than the second,
strncmpreturns a negative value. - Otherwise (the characters are both null characters),
strncmpreturns zero.
If the characters did not differ and neither was a null character, strncmp goes on to compare the following characters in the same way, until n pairs have been compared. If no difference has been found after n pairs have been compared, strncmp returns zero.
Some implementations of strncmp may return the signed difference between the two characters that differed, but this is not required by the C standard. strncmp may simply return +1 and −1 for “greater than” and “less than” or may use other positive and negative values.
strncmp(s1, s2, n) compares up to n characters from the strings pointed to by s1 and s2. It stops if it finds a difference and returns a negative value if the character from s1, converted to an unsigned char is less than the corresponding character from s2 (also converted to an unsigned char) and a positive value it is is greater. Subtracting the values of the differing characters is a handy way to compute a return value with the correct sign.
If no difference was found before the end of both strings or n characters have been processed, whichever comes first, it returns 0.
Your code has some small issues:
- the pointer arguments should be declared with type
const char *andnwith typesize_t. - similarly,
ishould be defined as asize_t, defining it as anintforces you to use a cast in the comparison and will cause undefined behavior for strings with an identical prefix longer thanINT_MAXand a large enoughnargument. - you should not dereference
s1[i]nors2[i]ifnhas reached0, so the test onnshould be performed first. - the difference should be computed as
diff = (unsigned char)s1[i] - (unsigned char)s2[i]; - note also that on some very exotic platforms, where
sizeof(char) == sizeof(int), the subtraction cannot be used as it may wrap around and produce incorrect results. - it is preferable to not use names reserved for standard functions for your own versions and it may cause clashes with the compiler's intrinsics.
Here is a modified version:
#include <stdio.h>
int my_strncmp(const char *s1, const char *s2, size_t n) {
for (size_t i = 0; i < n; i++) {
int diff = (unsigned char)s1[i] - (unsigned char)s2[i];
if (diff != 0 || s1[i] == '\0')
return diff;
}
return 0;
}
int main() {
char s11[] = "abcs";
char s12[] = "zzfs";
size_t n = 3;
printf(" strncmp() -> %d\n", strncmp(s11, s12, n));
printf("my_strncmp() -> %d\n", my_strncmp(s11, s12, n));
return 0;
}
The output values may differ but should have the same sign or should both be null.
I have an STM32756-EVAL board, and I would like to compare a uint8_t array to a char array.
uint8_t* inputCmd[5];
code_that_defines_the_inputCmd();
if (strcmp(inputCmd, "FIRMV") == 0){
do_something();
}
else{
do_something_else();
}Is strcmp in embedded C safe? If not, what are the usual substitutes?