The c/c++ strncmp signature is like the following:
int strncmp ( const char * str1, const char * str2, size_t num );
My question is what's the return value if num is 0? How the standard says? Don't find an answer from some online documents.
Thanks.
The return value from strncmp is 0 if the two strings are equal, less than 0 if str1 compares less than str2 , and greater than 0 if str1 compares greater than str2 (within the first maxlen characters). No other assumptions should be made about the value returned by strncmp .
The strncmp() built-in function compares at most the first count characters of the string pointed to by string1 to the string pointed to by string2. The string arguments to the function should contain a NULL character ( \0 ) marking the end of the string.
In the C Programming Language, the strcmp function returns a negative, zero, or positive integer depending on whether the object pointed to by s1 is less than, equal to, or greater than the object pointed to by s2.
strcmp compares both the strings till null-character of either string comes whereas strncmp compares at most num characters of both strings. But if num is equal to the length of either string than strncmp behaves similar to strcmp.
Well, strncmp
compares at most num
characters from the two strings, so with num == 0
, it compares none, hence finds no difference, thus it returns 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With