An implementation prototype of strcmp in the C language
- 2020-05-27 06:42:04
- OfStack
An implementation prototype of strcmp in the C language
Implementation code:
int __cdecl strcmp (
const char * src,
const char * dst
)
{
int ret = 0 ;
while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
++src, ++dst;
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return( ret );
}
Function prototype: int strcmp(const char *dest, const char *source);
Return value: returns an integer value if dest > source, the return value is greater than 0, if dest = source, the return value is equal to 0, if dest < source, the return value is less than 0. Character sizes are arranged according to the dictionary sequence of characters.
Parameter description: all characters ending with ''/0"
Implementation;
int strcmp(const char *dest, const char *source)
{
assert((NULL != dest) && (NULL != source));
while (*dest && *source && (*dest == *source))
{
dest ++;
source ++;
}
return *dest - *source;
/* if dest > source, The return value is greater than 0 If the dest = source, The return value is equal to 0 If the dest < source , The return value is less than 0 . */
}
The above is an example of the prototype of strcmp in C language. If you have any questions, please leave a message or go to the community of this website for discussion. Thanks for reading.