Jump to: navigation, search

Description

The strcmp() function compares the string pointed to by s1 to the string pointed to by s2.

Return value

The strcmp() function returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2.

Prototype

Declared in string.h

The C89/C99 prototype is:

int strcmp(const char * s1, const char * s2);

Implementation

In standard C, this can be implemented as:

int strcmp(const char* s1, const char* s2)
{
    while(*s1 && (*s1==*s2))
        s1++,s2++;
    return *(const unsigned char*)s1-*(const unsigned char*)s2;
}

Compilable unit, portable C90 in implementation namespace; public domain; past reviewers: none; current reviews: none

References

The C Standard, 7.21.4.2 (C99 numbering)

Personal tools