Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c/c++ what is the return value of strncmp when size is 0

Tags:

c++

c

size

strncmp

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.

like image 710
nowgains nowpains Avatar asked Jan 18 '13 21:01

nowgains nowpains


People also ask

What is the return value of strncmp?

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 .

What does strncmp mean in C?

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.

What does strcmp return?

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.

What is strcmp and strncmp in C?

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.


1 Answers

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.

like image 50
Daniel Fischer Avatar answered Oct 06 '22 00:10

Daniel Fischer