strcmp()
The strcmp()
function compares two strings and returns an integer value.
Note:
strcpy()
only works on C-style strings of typechar str[];
, not C++ style strings of typestring str;
.
Syntax
#include <string.h>
strcmp(string1, string2);
At the top of the file, the <string.h>
header file needs to be included. The strcmp()
function compares C-style strings string1
and string2
and returns an integer value.
- If the two strings are the same, it returns 0.
- If
string1
>string2
, it returns a positive value. - If
string1
<string2
orstring1
is a substring ofstring2
, it returns a negative value.
Codebyte Example
The following performs comparisons on three strings and prints out information about their order:
All contributors
- garanews222 total contributions
- StevenSwiniarski474 total contributions
- Christine_Yang271 total contributions
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.