strcmp [C++ Reference]
Keywords: c,string,strcmp
Date: 2008-10-14T15:42:02-0700

[[strcmp]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- cz
- de
- es
- fr
- it
- jp
- nl
- pl
- ro
- ru
- sk
- tr
- tw

## strcmp

Syntax:

        #include <cstring>
        int strcmp( const char *str1, const char *str2 );

The function strcmp() compares str1 and str2, then returns:

Return value | Explanation
--- | ---
less than 0 | `str1` is less than `str2`
equal to 0 | `str1` is equal to `str2`
greater than 0 | `str1` is greater than `str2`

For example:

         printf( "Enter your name: " );
         scanf( "%s", name );
         if( strcmp( name, "Mary" ) == 0 ) {
           printf( "Hello, Dr. Mary!\n" );
         }

Note that if str1 or str2 are missing a null-termination character, then strcmp () may not produce valid results. For a similar (and safer) function that includes explicit bounds checking, see strncmp().

Related Topics: memcmp, strcat, strchr, strcoll, strcpy, strlen, strncmp, strxfrm

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •